DDD: Correct way to get an entity from other subdomain?

93 views Asked by At

We have ordering and invoicing subdomain.

We need to access order in order to create invoice. What is correct way to access order from invoicing application service?

  1. Have invoicing application service have dependency on ordering service.
type invoicingService struct {
  orderingService OrderingService
}

func (s *invoicingService) CreateInvoice(orderID) (...) {
  order, err := s.orderingService.Get(ctx, orderID)
}
  1. Have invoicing application service have dependency on orders repository.
type invoicingService struct {
  orders OrderRepository
}

func (s *invoicingService) CreateInvoice(orderID) (...) {
  order, err := s.orders.Get(ctx, orderID)
}
  1. Have a custom Order entity/value_object inside invoicing subdomain? If this is correct solution, is there any point in having separate subdomains or it all should be one?

Thanks

1

There are 1 answers

0
VoiceOfUnreason On

Correct way to get an entity from other subdomain?

Often, the right answer is that you don't.

Which is to say, we typically want to share a copy of the information (ie: the current values of the entity) without necessarily also sharing the capability to change that information. In other words, we would typically share values rather than entities.

So most of your code will want to have some facade in place to hide the details of how providing an orderId produces the information that the invoices subdomain needs, but that facade won't necessarily be a repository (ie, won't necessarily be an abstraction the resembles an in memory collection of mutable entities).