The big idea really is “It’s okay to make objects that aren’t MVC”.
Here are some ways that thought can play out:
Service objects for handling domain model operations
Business processes often affect multiple domain entities. Stuffing process logic into models can result in the algorithm for processes being smeared across lots of files.
Having service objects that own the business processes centralise the algorithm and logic for a process in one place. This makes testing the process easy, and takes code out of your models (making them much simpler)
Controllers only handle HTTP wrangling
Don’t make your controllers do anything except call a service object and report on the result. This makes your controller tests simple and very very fast.
Decouple an asynchronous job from the action or task it performs
An async process has 2 components:
being scheduled
the task itself
If you implement this with 2 objects (a job class and a service object that does the work) then you have two sets of easy tests:
that the job calls the service (easy)
that the service does the work (easy)
I’m sure readers can suggest others.
I highly recommend Sandi Metz’ book POODR and her talk “the magic tricks of testing” for practical discussions of the big ideas.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks, indeed a great tip from you. What pattern you follow in such cases?
The big idea really is “It’s okay to make objects that aren’t MVC”.
Here are some ways that thought can play out:
Service objects for handling domain model operations
Business processes often affect multiple domain entities. Stuffing process logic into models can result in the algorithm for processes being smeared across lots of files.
Having service objects that own the business processes centralise the algorithm and logic for a process in one place. This makes testing the process easy, and takes code out of your models (making them much simpler)
Controllers only handle HTTP wrangling
Don’t make your controllers do anything except call a service object and report on the result. This makes your controller tests simple and very very fast.
Decouple an asynchronous job from the action or task it performs
An async process has 2 components:
If you implement this with 2 objects (a job class and a service object that does the work) then you have two sets of easy tests:
I’m sure readers can suggest others.
I highly recommend Sandi Metz’ book POODR and her talk “the magic tricks of testing” for practical discussions of the big ideas.