| Start |
|---|
| Service Class Controller is an entry point to our application. However, it’s not the only possible entry point. I would like to have my logic accessible from:
If I throw my logic into a controller it won’t be accessible from all these places. So let’s try “skinny controller, fat model” approach and move the logic to a model. But which one? If a given piece of logic involves A class which inherits from A service is just a regular Ruby object. Its class does not have to inherit from any specific class. Its name is a verb phrase, for example A service object does one thing A service object (aka method object) performs one action. It holds the business logic to perform that action. Here is an example: The three conventions I follow are: Services go under the
Benefits Service objects show what my application does I can just glance over the services directory to see what my application does: A quick look into a service object and I know what business logic is involved. I don’t have to go through the controllers, Clean-up models and controllers Controllers turn the request (params, session, cookies) into arguments, pass them down to the service and redirect or render according to the service response. Models only deal with associations, scopes, validations and persistence. This makes models and controllers much easier to test and maintain! When to use Service Class Reach for Service Objects when an action meets one or more of these criteria:
Sources Adam Niedzielski Blog (opens new window) |
Model Class Models are typically responsible for:
At the highest level, models describe domain concepts and manages their persistence. RemarksThis seems like a simple thing to do but when you're classes start ballooning in size you'll be thankful you took the time to organize them. |
Read Article
https://develop.horje.com/learn/1434/reference