rails-ddd-todolist
rails-ddd-todolist copied to clipboard
A sample project with efficiente OO techniques in Rails with Fast Test support
== DDD & OO architecture, applied to rails, with fast tests
This is a sample application (Todo list) of a Rails application with a clear separating of layers, while still supporting Active Record pattern usage. I believe it's very important for a mature product and system to clearly communicate is intent on a layered architecture, being a no-brainer for a new developer to the project.
We all know that Rails is about doing things fast, but we must learn to do the things maintainable, to avoid future headaches and allow the system to grow.
== Layers
We determined we need the following layers:
Frontend
-
Views: Just show the information on the desired formats
-
Controllers: Interact with the screen and the services (BusinessLogic)
-
Presenters: You may setup a presenter facade if needed.
Business
-
Services: A lot of the Business Logic resides here.
-
Models: PORO objects that although inherit from ActiveRecord:Base, the architecture allows to "decouple" logically. There is another option which is to manage ActiveRecord objects as Repository directly, totally splitting logic and model. We will also cover that in one case, just to check for advantages/disadvantages (TBD).
-
Repositories: Modules injected into the models, that will contain any method need to communicate with the DB. Again, to support current ActiveRecord and to avoid initial complexity we manage it with modules.
This an architecture definition to build applications on Rails or any technology, unfortunately the ActiveRecord coupling is very high on Rails (with its benefits) so we can not simply decouple it. (Rails 4?)
== ActiveRecord Rules
We should avoid using hooks. We should not depend on hooks for our business logic. All our business logic should be on the services or model based logic.
We will avoid the "chaos" having clearly defined logic on services and model and maintain logic free controller which indeed is not necessary.
== Sample
This is a very simple sample calling Businnes Logic method from the TodoController. You can find sample links in the default index.html page.
There is also a sample calling a Sidekiq background processed task to show how simple is to do it later on, without rewriting any code if we clearly separate our logic in several classes with Single Responsability Pattern.
You may run the test suite from a rake task: rake unit
== References
Some code is based and copied from @avdi book on Object on Rails which recommend to buy: http://objectsonrails.com/ I've also found this articles and videos remarkable if DDD is not your background:
- http://rubysource.com/ddd-for-rails-developers-part-1-layered-architecture/
and finally the best conference ever see from Uncle Bob that will blow your mind:
http://www.youtube.com/watch?v=WpkDN78P884
Hope this helps, @lcrodriguez