tag:blogger.com,1999:blog-7821856652257554779.post3665408153526462451..comments2025-09-20T09:27:07.510+02:00Comments on Andrzej on Software: An application or a set of resources?Andrzej Krzywdahttp://www.blogger.com/profile/06399276063142826365noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7821856652257554779.post-59042816776203026652011-02-15T16:08:16.245+01:002011-02-15T16:08:16.245+01:00Hi Andrzej Commands are only used to modify data....Hi Andrzej<br /><br />Commands are only used to modify data. For index and show actions we access the models &quot;the rails way&quot; directly in the controller (in simple cases) or we use presenters to transform complex object graphs from the domain model to the view model. <br /><br />I think that the command pattern is not really needed if you have a very simple application. But when your domain model and business logic reaches a certain degree of complexity, it helps to manage that complexity.<br /><br />Another benefit: Power users can use our commands directly within a &quot;console&quot; in our web application. They love it.<br /><br />Best regards<br />GernotGernothttps://www.blogger.com/profile/08836163611074377149noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-68016627661142900452011-02-15T14:41:37.605+01:002011-02-15T14:41:37.605+01:00@Gernot I like it very much! I think you&#39;re u...@Gernot<br /><br />I like it very much!<br />I think you&#39;re using the Context part from the DCI approach, see my previous blog post:<br /><br />http://andrzejonsoftware.blogspot.com/2011/02/dci-and-rails.html<br /><br />I want to experiment with it as well.<br /><br />I think your Commands part is something that is popular in the Java/.Net world as the Application Logic. It&#39;s not very popular in the Rails world, though.<br /><br />As for the application vs resources discussion - when you want to display all the users from your app, you simply call User.all directly from the Command?Andrzej Krzywdahttps://www.blogger.com/profile/06399276063142826365noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-63285234748596575462011-02-15T13:23:46.649+01:002011-02-15T13:23:46.649+01:00The rule of thumb is that there is a command for e...The rule of thumb is that there is a command for each CRUD operation on a resource.<br /><br />Here&#39;s an example from my problem domain, a rental contract. We have a resource rental_contracts. To create a new rental contract, we have a view that allows the user to select one or more people (the tenants), one or more objects to rent and several attributes for the contract.<br /><br />Since the construction of such a contract is quite complex and carries a lot of business logic, we have a create_rental_contract command. The command accepts a params hash for its constructor, just like a model. Furthermore, it includes ActiveModel::Validations so it can validate the params.<br /><br />In short, a command behaves like a model from the controller&#39;s point of view. The controller simply passes the params hash to the command, executes it and redirects to the &quot;new&quot;-view if there were validation errors. All validations are implemented in the commands, not in the models.<br /><br />The create_rental_contract command then orchestrates the required state changes in the domain model. It creates the contract, adds the tentans and objects to the contracts, sets the contract objects to &quot;not available for rental&quot; and so on. <br /><br />This approach imho has several advantages:<br /><br />- Commands are easily testable<br />- Commands add context <br />- Commands encapsulate business transactions<br />- Commands do the transformation from the view model to the domain model (the view model is oftentimes simplified and hierarchical (Parent/Child) while the domain model is object oriented)Gernothttps://www.blogger.com/profile/08836163611074377149noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-28971139415713409872011-02-15T12:22:41.805+01:002011-02-15T12:22:41.805+01:00&quot;The business logic is implemented in command...&quot;The business logic is implemented in command classes. Only commands can change the state of objects. The controllers talk to commands, never directly to the domain model.&quot;<br /><br />It sounds interesting. Could you show an example of controller-command-model communication?Andrzej Krzywdahttps://www.blogger.com/profile/06399276063142826365noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-257906397159138012011-02-15T11:48:40.115+01:002011-02-15T11:48:40.115+01:00After I really understood the REST philosophy (at ...After I really understood the REST philosophy (at least I think I got it), I see my applications as a set of resources and URIs to access these resources.<br />Users are resources, too. I do not model my applications user centric. Instead, I restrict the accessablility of resources based on an authorization model.<br />The authorization model defines what a user can see and do. <br /><br />The business logic is implemented in command classes. Only commands can change the state of objects. The controllers talk to commands, never directly to the domain model.<br /><br />This is a common architecture and it works really well for me, especially using the Rails framework. It scales well, is easy to understand and test.Gernothttps://www.blogger.com/profile/08836163611074377149noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-10749016961307714292011-02-14T09:16:19.972+01:002011-02-14T09:16:19.972+01:00A student of Zen once asked Buddha &quot;Do you th...A student of Zen once asked Buddha &quot;Do you think of your web application as an application or as a set of resources?&quot;.<br /><br />Buddha replied &quot;Do you think of your online resources as resources or parts of an application?&quot;<br /><br />At that moment, the student was enlightened.tawhttps://www.blogger.com/profile/16972845140253292628noreply@blogger.com