When does logic belong in the Business Object/Entity, and when does it belong in a Service?

I would tend to have an external service determine the shipping rate. For me that's application logic rather then order-specific logic. For example you might decide for a period to offer free shipping for orders over a certain size, or to a particular group of loyal customers.

For me that logic will tend to change independently of how an order is constructed Most likely I would have the code which is responsible for placing the order (some kind of Order Processor service, in an application layer or in a command handler) hand off to a service to get the shipping rate, and then pass that rate into the order, so I guess option 2 For printing a shipping label, I'd tend towards having the domain raise an event along the lines of udidahan.com/2009/06/14/domain-events-sa... A separate handler would then deal with printing the label. Again, the logic for this is that the way you print labels is likely to vary independently of how you construct an order, so it makes sense to keep that separate. Using a domain event seems to be the cleanest way of ensuring that the label is printed at the right time without requiring the Order (or indeed the order processor) to be aware of the printing logic.

I would tend to have an external service determine the shipping rate. For me that's application logic rather then order-specific logic. For example you might decide for a period to offer free shipping for orders over a certain size, or to a particular group of loyal customers.

For me that logic will tend to change independently of how an order is constructed. Most likely I would have the code which is responsible for placing the order (some kind of Order Processor service, in an application layer or in a command handler) hand off to a service to get the shipping rate, and then pass that rate into the order, so I guess option 2. For printing a shipping label, I'd tend towards having the domain raise an event along the lines of URL1 separate handler would then deal with printing the label.

Again, the logic for this is that the way you print labels is likely to vary independently of how you construct an order, so it makes sense to keep that separate. Using a domain event seems to be the cleanest way of ensuring that the label is printed at the right time without requiring the Order (or indeed the order processor) to be aware of the printing logic.

– Casey Jan 30 at 0:50 I worried about that as well, but it hasn't really panned out that way. Definitely some logic has ended up at the application level, but I actually think that's right anyway. – David Jan 30 at 1:10 Just reading over your original question again.

If you're calling an external webservice for shipping rates, then that logic is outside the domain regardless. I'd say it's an application-level decision to use an external webservice. If there were rules that you applied to the information you got back - eg you apply a discount to the rate for loyal customers or something - that logic might be still applied in the domain, so then you'd pass the result of the web service back into the domain.

– David Jan 30 at 1:23 Thanks for the input David. Your answer prompted me to review some DDD principles, and I think I am trying to stuff too much behavior into my domain entities when that behavior isn't really related. I would agree with the other posters here that 2) is the best solution for this case.

– Casey Jan 30 at 22:23.

If you are accessing external webservices to get Postage rate, it is better to create interface in Application layer, because evan itself suggested that if you want to talk with external webservices you should construct interface in Application layer, you would have the service implementation injected into your Domain Object. For printing shipping label, because label is printed only when postage rate is determined, so is a kind of event like PostageRateConfirmed your domain can raise this event. danhaywood.com/2010/04/30/accessing-doma....

I would use (2). It doesn't add extra complexity to your Order Item. To me, it seems the natural use of a helper service.

Update: in response to comment: The wiki page states: Anemic Domain Model: With this pattern, logic is typically implemented in separate classes which transform the state of the domain objects.

– Casey Jan 30 at 0:03 I don't think so, as far as I uderstand it, it is OK to have a seperate object take a bunch of other objects and make decisions based upon their state as long as the objects in question are not being altered or transformed. – Mitch Wheat Jan 30 at 0:18 If we change the state of the order by having the service set a PostageRate property on the order... Does that count as transforming the order? Does something that insignificant mean that we would change our original decision?

It seems that DDD is so full of these "Catch 22's" that it becomes very hard to actually implement. – Casey Jan 30 at 0:44 I would question whether PostageRate belongs to the Order Item. Perhaps it belongs to a PostedOrder( – Mitch Wheat Jan 30 at 0:47.

My point of view: The domain is what contains the logic of your application, without the infrastructure cruft. The logic is that when an order is confirmed a label is printed and the shipping rate is determined. That should be in the domain.

The infrastructure then accomplishes what the domain wants to do. The domain may let the infrastructure know through messaging, or events. That way no infrastructure leaks into the domain, you only require a way to transport messages out of the domain.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions