Reputation: 3289
I am a bit confused about the Domain layer in the Domain Driven Design architecture.
I understand that the four layers are Presentation, Application, Domain and Infrastructure, where Infrastructure contains the data repositories.
I also understand that the Domain layer is responsible of the business rules. In the now dated anemic model, the domain objects didn't have any behavior. In DDD, we are supposed to move the behavior and business rules from services to Domain.
Before, I would inject repositories to the services layer. So my question is - is it okay to inject repositories to the Domain objects so they can perform the business rules?
Upvotes: 4
Views: 269
Reputation: 13256
No, it is not okay to inject repositories into domain objects :)
What is acceptable, but only if there really is no other way, is to pass in repositories or other domain objects such as services into an AR method to perform some function via the double-dispatch method:
public void ApplyDiscount(IDiscountService service)
{
_discount = service.Discount(customerType);
}
As I have mentioned in other posts, I tend to think of an AR as I would a physical calculator. There is input, via the keypad, and there is output, via the screen. While the calculator is doing its voodoo it doesn't interact with anything else and doesn't ask for additional information. That being said, there are probably going to be exceptions as in the example above but I guess the discount could be determined by the operation script (service layer):
public void RegisterOrderTask
{
private IDiscountService _discountService;
private IOrderRepository _orderRepository;
public void RegisterOrderTask(IDiscountService discountService, IOrderRepository orderRepository)
{
_discountService = discountService;
_orderRepository = orderRepository;
}
public void Execute(OrderDetails details)
{
_orderRepository
.Add(details.CreateOrder()
.SetDiscount(_disocuntService.Discount(details.CustomerType)));
}
}
These are just some made up ideas but may get you thinking about your scenario :)
Upvotes: 3