Ruby on Rails Friday, January 9, 2015



Although I do see it work well in some cases, I also see state machine implemented badly in other cases. In particular, I find that it encourages an over-use an over-reliance on the callback hook pattern (in the context of AASM, attaching methods to specific transitions).

In smaller implementations it can work well, but as the complexity of domain logic grows it often breaks down and leads to difficult to debug code (and smelly code). 

So with all things Rails, your mileage may vary. I would suggest reading up on DCI and thinking about how it might look to build an object (in the context of DCI, this is called a "context object") that represented all the things the state transition does itself. For example, if you were going to ship a shipment, you would want to 1) change its state, 2) mark it shipped_at, 3) send an email, 4) etc, etc. 

The idea behind DCI is that we consider the transition itself to be domain knowledge and give it a first-order class (although certainly not an Activerecord object) in our app. That's the basic gist of it.  It is also possible to use a state machine pattern alongside DCI or DCI-like patterns. I think the questions is, how much do you use State machine and how much do you rely on attaching hooks to your transitions. 

-Jason



On Jan 8, 2015, at 11:14 PM, hai nguyen ngoc <nguyenngochai.shipagent@gmail.com> wrote:



I am searching AASM gem to apply my demo project but I am wondering when I really need to use it and use it in what case? What is advantages and disadvantages?

So if someone has experiences on it, could you tell me know.

Best regards,

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d26c47d3-54e5-4fe4-b461-52b82bc21f92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

----

Jason Fleetwood-Boldt

All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email jason@datatravels.com with questions/concerns about this.

No comments:

Post a Comment