Share
Embed
DCI And Rails (Jakarta.rb February 2014)
My presentation during Jakarta.rb meetup on February 2014 about "DCI and Rails"
Giovanni Sakti
February 19, 2014
Transcript
59 slides
-
I’m having problem with remembering people faces and names !
self-diagnosed with mild OCD ! 70% of my time during work is spent on reviewing codes from collaborators 6JKUKUOG -
My Main Concern Ensure that codes is as comprehendible as
possible, make sure that any people whom participate in the project can understand and contribute effortlessly even though the project grows larger and larger and larger ! RGTHQTOCPEGOC[GXGPDGCUGEQPFEKVK\GPKPUQOGECUG -
I’m (currently) not a DCI proponent ! I’m (still) not
using it for production (only experimenting with it) ! I understand that Rails still can’t support it 100% yet -
Common Rails developer evolution on “where to put my logic?”
! QTKPUGTVCP[QVJGTYGD/8%HTCOGYQTMU .GVņUVCMGCNQQMCV -
<% people = Person.where("added_at > ? AND deleted = ?",
Time.now.utc, false).order("last_name, first_name") %> <% people.reject { |p| p.address.nil? }.each do |person| %> <div id="person-<%= person.new_record? ? "new" : person.id %>"> <span class="name"> <%= person.last_name %>, <%= person.first_name %> </span> <span class="age"> <%= (Date.today - person.birthdate) / 365 %> </span> </div> <% end %> -
<% @people.each do |person| %> <div id="person-<%= person.new_record? ? "new"
: person.id %>"> <span class="name"> <%= person.last_name %>, <%= person.first_name %> </span> <span class="age"> <%= (Date.today - person.birthdate) / 365 %> </span> </div> <% end %> class PeopleController < ActionController::Base def index @people = Person.where("added_at > ? AND deleted = ?", Time.now.utc, false).order("last_name, first_name") @people = @people.reject { |p| p.address.nil? } end end -
class Person < ActiveRecord::Base def self.find_recent people = Person.where("added_at >
? AND deleted = ?", Time.now.utc, false).order("last_name, first_name") people = people.reject { |p| p.address.nil? } end ! def name "#{last_name}, #{first_name}" end ! def age (Date.today - person.birthdate) / 365 end ! def pseudo_id new_record? ? "new" : id end end -
<% @people.each do |person| %> <div id="person-<%= person.pseudo_id %>"> <span
class="name"><%= person.name %></span> <span class="age"><%= person.age %></span> </div> <% end %> class PeopleController < ActionController::Base def index @people = Person.find_recent end end -
But, was anything fat ever good in our app codebase?
! +UVKNNFQPņVMPQYVJGCPUYGTHQTVJKU -
Class User << ActiveRecord::Base ! def process1_method1
… end ! def process1_method2 … end ! end -
Class User << ActiveRecord::Base ! def process1_method1
… end ! def process1_method2 … end ! def process2_method1 … end ! def process2_method2 … end ! end -
Class User << ActiveRecord::Base ! def process1_method1
… end ! def process1_method2 … end ! def process2_method1 … end ! def process2_method2 … end ! def process1_method3 … end ! def process1_method4 … end ! def process3_method1 … end ! def process3_method2 … end ! end -
Class User << ActiveRecord::Base ! def process1_method1
… end ! def process1_method2 … end ! def process2_method1 … end ! def process2_method2 … end ! def process1_method3 … end ! def process1_method4 … end ! def process3_method1 … end ! def process3_method2 … end ! def process1_method1 … end ! def process1_method2 … end ! def process2_method1 … end ! def process2_method2 … end ! def process1_method3 … end ! def process1_method4 … end ! def process3_method1 … end ! def process3_method2 … end ! def process1_method1 … end ! def process1_method2 … end ! def process2_method1 … end ! def process2_method2 … end ! def process1_method3 … end ! def process1_method4 … end ! def process3_method1 … end ! def process3_method2 … end ! end -
Class User << ActiveRecord::Base include Commentable
include Taggable include Manageable include ShoppingCartable include BlahBlah …etc end -
9JCVņUYTQPIYKVJVJKU! Class User << ActiveRecord::Base include Commentable
include Taggable include Manageable include ShoppingCartable include BlahBlah …etc end -
Developer still have to makes sense what goes where !
Business Process cannot be evaluated procedurally -
Wait.. ! If we use BDD we can see and
build business processes using the integration test, right? -
Wait.. ! If we use BDD we can see and
build business processes using the integration test, right? 6JCVKU%QTTGEV -
DCI is about assigning roles to a model in a
context /[WPFGTUVCPFKPIQH&%+ EQPVGZV#-#WUGTUVQT[ -
A model must not have any business process related methods..
! business process and model must be tightly separated.. -
Class User << ActiveRecord::Base include Commentable
include Taggable include Manageable include ShoppingCartable include BlahBlah …etc end ;GCJVJKUKUYTQPIKP&%+ -
So where should we put our code then? ! not
in views.. not in controllers.. not in models.. not in concerns.. -
class User < ActiveRecord::Base validates :name, :presence => true end
! class Book < ActiveRecord::Base validates :title, :presence => true end 6JKUKUQWTOQFGNU -
6JKUKUQWTEQPVGZV class AddToCartContext attr_reader :user, :book ! def self.call(user, book)
AddToCartContext.new(user, book).call end ! def initialize(user, book) @user, @book = user, book @user.extend Customer end ! def call @user.add_to_cart(@book) end end -
class AddToCartContext attr_reader :user, :book ! def self.call(user, book) AddToCartContext.new(user,
book).call end ! def initialize(user, book) @user, @book = user, book @user.extend Customer end ! def call @user.add_to_cart(@book) end end -
class AddToCartContext attr_reader :user, :book ! def self.call(user, book) AddToCartContext.new(user,
book).call end ! def initialize(user, book) @user, @book = user, book @user.extend Customer end ! def call @user.add_to_cart(@book) end end 2GTHQTOCPEGRTQDNGOJGTG -
What I showed you was not an official method to
do DCI in Rails. ! In fact, no official method exist yet. -
“This approach to breaking up domain logic into concerns is
similar in some ways to the DCI notion of Roles. It doesn’t have the run-time mixin acrobatics nor does it have the “thy models shall be completely devoid of logic themselves” prescription, but other than that, it’ll often result in similar logic extracted using similar names.” -
Imagine a time when business process related code can be
placed exactly within the actual code (not the test). ! And tightly separated from the model. 9GNN+ņOIQPPCNQUGO[LQDDGECWUGO[ENKGPVUECPEQFG EQORNKECVGFDWUKPGUURTQEGUUD[VJGOUGNXGUKHVJCVJCRRGPU -
Ruby and Rails still have long ways to go to
support DCI completely ! But you can still use it now if you want to.. (QQFHQTVJQWIJVU -
http://signalvnoise.com/posts/3372-put-chubby-models-on- a-diet-with-concerns ! http://www.sitepoint.com/dci-the-evolution-of-the-object- oriented-paradigm/ ! http://mikepackdev.com/blog_posts/24-the-right-way-to- code-dci-in-ruby ! http://dec0de.me/2013/02/dci-performance/
! “Clean Ruby v0.9” - Jim Gay 4GHGTGPEGU