Domain Model vs Service in a MVC context

design-patternsdomain-objectsobject-oriented-designservice

At the end of this article from Martin Fowler there is this sentence:

In general, the more behavior you find in the services, the more likely you are to be robbing yourself of the benefits of a domain model. If all your logic is in services, you've robbed yourself blind.

What the whole, very abstract article, doesn't tell you is what exactly goes into the service layer?

As I undestand it the architecture would be something like:

Controller -> Service(s) -> Domain Object(s) -> Repository Object(s)

So what kind of code would go into the service layer if the domain objects implement all the business logic? What would be the difference between the controller and the service layer if the service is not implementing business logic?

Best Answer

It all sort of rests on where you want to do your coupling and how loose you want it to be. Also, it's not like any of these are absolutes. But if you want to be super DDD about it, thoughtfully consider what a service is. "Edge" services are probably your entry point into an app, but then you have "domain" services below that.

EdgeService -> Domain Object -> Domain Service -> Repository

If your domain objects hold a bunch of domain services that implement strategies for doing "verbs" on each domain object then you've got sort of a regular pattern.

so your edge service would take a blob of json, and make a cow out of it then when you call cow.save() cow finds it's validator service, validates it self, maybe calls it's moo service to moo at the herd, and then uses a repository object to put itself in the barn.

Anywhere you can get a cow object, you have access to all of the functions it presents without having to worry about the collaborating domain services etc: The cow interface would just expose moo() and milk() methods which is all the farmer needs to know about.