0

I have two aggregates Order and Customer. Order has a identity reference to customer.

public class Customer
{
   public long CustomerId { get; private set; }
   public string Name { get; private set; }
   public Address Address { get; private set; }
   ...
}

public class Order
{
   public long OrderId { get; private set; }
   public long CustomerId { get; private set; }
   public Date OrderDate { get; private set; }
   ...
   public string CustomerName { get; priate set; } //from Customer Aggregate
   ...

   public void AssignCustomer(Customer customer)
   {
      this.CustomrName = customer.Name
   }
}


As you see in Order aggregate I need CustomerName plus identity reference, and for this reason I added a method AssignCustomer to set it from outside of Order aggregate. Is this approach OK from DDD perspective?

If that's accepted, I have another problem. Suppose in OrderRepository there is a method that returns list of Orders like this :

public interface IOrderRepository
{
   IList<Order> GetAllOrderWithDate(Date date);
   ...
}

Now if I want to set CustomerName for all these orders, the N+1 problem would occur, cause I need to read related Customer for each Order in list.

Is there any proper solution in DDD?

Saeed
  • 185
  • 2
  • 13

0 Answers0