1

After spending endless hours on the web (and in books) trying to come to a conclusion about this subject, looking through many people's point of views, and different aspects trying to weight pros and cons, I have decided to post some key questions that I hope some people smarter than me will answer :)

I read Martin Fowler's articles about ADM (Anemic Domain Model as he calls it) as well as books, and I know about Eric Evan's DDD (Domain Driven Design). They are very respectable, experienced architects, and they have done an extraordinary job compiling all these knowledge into their books and articles, however (and I know this is nearly impossible to do, because it is like that in all printed media) their examples are usually very clear cut, which is ok to explain a concept, but unfortunatelly it is hard to put them into real life use.

Here, I will quickly explain a few cases that I am very interested in your opition in (RDM / ADM+TS(Service)), let's assume an IoC container is doing wiring (Although it is mostly irrelevant):

Case I/1:

TASK: Placing an order

ADM+TS:

REQUIREMENTS:
Order oject - data (set+get) (DTO-like "data bag")
OrderService - operations (TS-like operations over an entity object)

RDM:

REQUIREMENTS:
Order oject - data and functionality (Rich)

Case I/2:

TASK: placing order and send an email after it

ADM+TS

REQUIREMENTS:
Order oject - data (set+get) (DTO-like "data bag")
OrderService - operations (TS-like operations over an entity object)
EmailService - to send emails
(optional) OrderServiceEmailDecorator - to separate the concern of actually placing the order, and sending an email

COMMENTS:
Solutions:
a. Use the existing OrderService and add emailing to it, in which case the OrderService depends on EmailService
b. Separate the concern into a Decorator that we can wire together with th service in the IoC and use on-demand based on if we want this or not

RDM:

REQUIREMENTS:
Order oject - data and functionality (Rich)
? EmailService - to send emails
? EventHandler - to catch events

COMMENTS:
Well, in this case people usually recommend the following things:
a. "Inject your dependencies into the domain layer": That would make the domain layer very heavy, and full of dependecies all around the place.
b. "Pass the service along with the place(...) call": That would make the function signature change all the time as more and more dependecies are going into it.
c. "Raise an event that a significant operation was completed": Even the strongest RDM advocates say that persistence should not be directly in the domain model, meaning that we are raising an event here, however the operation was not full executed (persisted). so we might be sending an email before it is done. We could say the email can fail so it is not perfect either way, but I think actually placing an order is the main operation here, sending an email is just a notification, and also, that can be repeated, plus you get on-screen notifications etc. You get the point here, really, placing an order is not dependent on being able to send the notification email, but you defeinitely don't want to send th email if persisting the order fails. BUT some can say, raise events like that in the Repository where it is getting persisted, that's more like it, however that spreads these events around.

Case I/3:

TASK: Orders can be placed in bulk, and we only want to send one notification email with all the orders in it (Please do not start commenting on how these are just items on the same order, this is an example).

ADM+TS

REQUIREMENTS:
Order oject - data (set+get) (DTO-like "data bag")
OrderService - operations (TS-like operations over an entity object)
BulkOrderService - will depend on OrderService (non decorated)
EmailService - to send emails
BulkOrderServiceEmailDecorator - To depend on EmailService for sending the aggregated email

COMMENTS:
Instead of using the decorated OrderService, we use the (Decorated) BulkOrderService

RDM:

REQUIREMENTS:
Order oject - data and functionality (Rich)
? EmailService - to send emails
? EventHandler - to catch events

COMMENTS: Our domain object is getting a bit complex now, we cannot put a .bulkPlace() on it because obviously I want to place multiple orders in the same context sothat logic has to exist one layer up, let's say on the controller level, and call each place() on each order, in which case: Continuing from above (baes on I/2 RDM solutions):
a. ("Injected dependecies") How do we get around the email sending here? Now we need a .placeWithEmailAndAnyOtherDependency.. and a .placeWithout...? You can't exactly decorate domain objects to be fair
b. ("Pass the service") Now here you can maybe do that if you pass null instead of the service it wouldnt send the email (but that seems dodgy)
c. ("Raise event") This is a problem, now that we tied email sending to this event, and we want to reuse the .place() call, even on bulk orders there will be multiple emails sent, unless we can detach it somehow (can't really decorate the Repository either)

Now some of these issues can probably be resolved with AOP instead of decorators, but still it feels hackish.

Case I/4:

TASK: Now we have multiple entry points, given that we want to be able to "schedule" a recurring bulk order from our scheduler, but also want to keep this functionality on our website directly. (Or I can just say we ant to have a console client as well as the web client whatever, point is our webcontrollers won't do the job, not directly anyways)

ADM+TS

REQUIREMENTS (Unchanged):
Order oject - data (set+get) (DTO-like "data bag")
OrderService - operations (TS-like operations over an entity object)
BulkOrderService - will depend on OrderService (non decorated)
EmailService - to send emails
BulkOrderServiceEmailDecorator - To depend on EmailService for sending the aggregated email

COMMENTS: Instead of using the decorated OrderService, we use the (Decorated) BulkOrderService - essentially nothing changes

RDM:

REQUIREMENTS:
Order oject - data and functionality (Rich)
? EmailService - to send emails
? EventHandler - to catch events

COMMENTS: Depends on what we did in I/2, the same problems that I/3 had still apply, but on top of it, we cannot use our controller anymore to loop though the orders, or if we do it turns into something like a TS and we are back to a similarly layered architecture that we get with ADM+TS

So, my main problem is that I couldn't find a definite, well fitting solution in RDM for a simple issue like this myself, even after reading and google-ing people recommend different things, that are good to solve one thing, but bleeding from another, while the ADM+TS solutions feel more flexible dealing with them. (Not to mention the fact that you don't need DTO-s, because your ADM is essentially your DTO that you can pass around event to the view - so no transformations neccessary)

If you have an opinion about how to handle (progressively) Case I/2 and /3 with an RDM in a way that feels fitting, please leave a comment, however if you do, please provide and answer for all the issues (all 4, or at least the last 3, since 1 is not really an issue)! Not just the ones you have a convinient answer to (like half the tasks/etc)

Thanks

UPDATE: Seeing some answers, I probably should have picked a different "entity" then the famous Order for this excercise (I just wanted to pick a familiar one). Anyway, as an addition, try to imagine that Case I/2, I/3, I/4 were NOT initial requirements, they kind of evolved organically. These requirements were added step by step. So first you were told to send an email whenever there is an order, now if you coupled these together in any way, you will have a problem when I/3 hits with the bulk order. Even if you just put the email on a messagebus and it wasn't just sent yet, what do you do on bulk? then you put the message on the bus and then remove it/doing cleanup? Or with any other action that based on I/2 should be triggered, but based on I/3, not applicable anymore, just do them anyway then revert them? That doesn't sound right

2 Answers 2

0

Case I/2

To address the premature sending of emails in response to failed domain events take a look here.

Case 1/3

If the placement of bulk orders is a use case then make it explicit in the domain. Regardless of whether you use a domain model or a transaction script, you still have an application service. In turn, this application service would have a method implementing the bulk order placement use-case. As far as the domain model, you can potentially create a BulkOrder aggregate with which existing orders can be optionally related.

Overall, I think your idea of a rich domain model is too strict. Even with a rich domain model, you still have coordinating application services. Also, refactoring to a domain model often provides deep insight into the domain itself by forcing implicit concepts to become explicit.

0

An Order is always a document specifying what products and quantities the client ordered. For this reason it's immutable so we can't speak about too much rich behavior. But it's important that the Order is a business concept, it doesn't need to have 20 methods. For prices, totals and taxes there is the business concept of Invoice attached to an Order.

Case I/1, I/2

I don't see the problem in using Domain Events. Let's say the order has been submitted

 class ManageOrderHandler:IExecute<CreateOrder>
  {
   public void Execute(CreateOrder cmd)
   {
         Order order=someFactory.CreateOrder(cmd); //not important
         _repository.Save(order);
         _bus.Publish(new OrderSubmitted());
    }
  }

The event is publish only after the order has been persisted.

 class Notifier:ISubscribeTo<OrderSubmitted>
 {
       NotifyService _service;//constructor injected

        public void Handle(OrderSubmitted evnt)
        {
             _service.Notify(/* relevant parameters */);
        }
  }     

  class NotifyService
  {
        public NotifyService(ISendEmails emailNotifier, ISendSms smsNotifier /* etc */)
        {}

        public void Notify(/* arguments */)
        {
             // depending on settings and the arguments send email and/or sms and/or others
        }
  }

Of course, the NotifyService (excuse the poor naming) can be directly a handler of OrderSubmitted, but right now I prefer to have things as decoupled as they can be.

Case I/3, I/4

Kinda same things but adjusted. The Repository accepts more than one Order so all orders will be considered a transaction. The published event will be BulkOrdersSubmitted containing the relevant data. The NotifyService is unchanged.

In fairness, I worry much more about modeling the Domain correctly.Using a message driven architecture allows you to write things decoupled and it's much easier to adjust or to implement new features. But for that you really need to get that Domain modeling right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.