- Can some one give me a good example of domain service
- are they supposed to be state-less.
- are they equivalent to transaction script.
- Can a report generation service be call as domain service.
1 Answer
A domain service seems to be something kind of dark, some stateless object that DDD makes us write because in order to us to get the reference to the aggregate. It all sounds pretty technical, but instead it is actually pretty easy and let me start:
1) A domain service is a domain object. This means that it is an object of your domain in analysis (bounded context). Bertrand Meyer said once, "the objects are there, you only have to identify them". The confusion with DDD is that everyone that is trying to understand every term pay much attention to the DDD names and forget about the domain. DDD is doing OOP, the ONLY difference is that DDD fixes some names for DOMAIN objects that play a given role within a domain (bounded context). Let me show you:
User story: "Generate an invoice for phone calls of a customer".
Objects identified: Biller Invoice PhoneCall PriceList PhoneCallRegistry
As you see those are the domain objects identified for the user story and the names we could use for writing the code. Howeveor, if we use DDD we have to rename some of those objects, lets see how it gets:
BillingService Invoice PhoneCall PriceList PhoneCallRepository
As you saw two names changed. You can see now that a domain service is the guy performing the service for accomplishing some task, in this case the Biller. Second, the PhoneCallRegister plays the role of a collection some it is compliant with what a repository is in terms of DDD, so we renamed it to PhoneCallRepository.
DDD is not a new thing, it is just doing OOP, but it make us rename some objects of our domain, but you could use Biller or PhoneCallRegistry and you are still doing DDD. This is why it is difficult to understand DDD.
2) Domain services are stateless, if you take into account what I said in the previous point you can see a guy that fills an invoice plays the role of a biller, the guy is just there sit down in his desk doing his work attending serving many many "threads". if any other guy replace him we do not care, we just want to phone calls to be invoiced.
3) Not at all. Transaction script is procedural code that gets into legacy code. The biller/billingService is a domain object. I think you understand now by yourself after reading the previous points hether it is transaction script or not.
4) Based on point 1 the answer is yes. However there are some times that a view needs to be presented with complex cross data from several bounded context and you might have a denormalized model for this purposes. In this case you will see that there in no object of your domain that accomplishes this task, so you can directly collaborate from your application layer (like a rest service) with the repository to perform the query and report that to the view. You can call this object "ReportingService".
Hope it helps.
Sebastian.
-
Nice answer, but for #4 I'd say that a reporting service would be a Domain service only if the Report generation depends on some business rules or if the Report is a business concept with a specific definition. For UI purposes, the report is just a result of some queries and its generation would stay in the app layer, however it wouldn't use a Domain repository to perform the said queries. It would use something of a query specific repository (Dao, service, ORM etc) that the Domain doesn't know about. This is because you don't want UI responsibilities on your Domain repo.– MikeSWNov 30, 2014 at 16:56
-