user2309367
user2309367

Reputation: 327

Should domain models call infrastructure interfaces?

is the following good design and allowable in onion architecture and domain driven design?

say you have an "Order" domain class like so

class Order
{
  INotificationService _notificationService;
  ICartRepository _cartRepository;

   void Checkout(Cart cart, bool notifyCustomer)
   {
         _cartRepository.Save(cart);
         if (notifyCustomer)
         {
            _notificationService.sendnotification();
         }
   }
}

Is it good or bad design to have the domain model call interfaces of infrastructure?( in this case, notificationservice and CartRepository)

Upvotes: 3

Views: 1049

Answers (4)

Arash
Arash

Reputation: 3688

repository belong to infrastructure layer also it would be better to use Icart interface

void Checkout(ICart cart, bool notifyCustomer)

to reduce coupling.

Upvotes: 0

Ehsan
Ehsan

Reputation: 834

Repository layer are exist for each aggregate of the domain,repository is on top of domain, you can have repository base interfaces on your domain who they live in infrastructure but you can not see the repository implementations in your model(it's not correct). there are some basic interfaces such as UnitOfWrok, Repository, Specificationm authentication and ... in your infrastructure accessible in all layers. I recommend to take a look at aghata store front written in .net to see a real implementation project https://github.com/elbandit/Asp-Net-Design-Patterns-CQRS

Upvotes: 0

MaxSC
MaxSC

Reputation: 4738

Your design will be OK only if both INotificationService and ICartRepository interfaces are defined within your Domain (Core) layer and if they're bound at runtime with the right implementation by your Dependency Resolution layer (the outermost layer of your Onion architecture).

Remember that in an Onion architecture, your Domain layer cannot reference any libraries.

ICartRepository implementation will obviously be done in your Infrastucture layer as it will surely be tied to your Data Access layer technology.
If your INotificationService implementation needs to talk to an external service, then it goes to Infrasrtructure as well. But if it is part of your business then it's implementation could be in the Domain layer.

Upvotes: 2

Yugang Zhou
Yugang Zhou

Reputation: 7283

I think INotificationService is a domain concept rather than infrastructure service. We could model this as a DomainEvent telling "the customer about the cart is saved".

What about moving the INotificationService to the domain layer and rename it as "CartDomainEvents".

CartDomainEvents.raise(CartSavedEvent(...));

In general case, Calling infrastructure components introduces bidirectional dependencies which is usually not a good design.

Upvotes: 1

Related Questions