question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question]: DDD to remove an order item

See original GitHub issue

Hi,

I’m looking at DDD and how we can remove items from a collection.

In the Order microservice the Order entity has a list of OrderItems which can only be added to via the Order aggregate method.

How would we go about removing an order item from the Order? If we go the same route of encapsulation then we would have a method such as:

public void RemoveOrderItem(OrderItem orderItem)
{
    _orderItems.Remove(orderItem);
}

But this wouldn’t actually remove it as it needs to be removed via Entity Framework’s Remove() method on the DbContext.

So this would require either a new IOrderItemRepository or an additional method on the IOrderRepository to make use of the DbContext.

However, this breaks the encapsulation pattern as it can be done from outside the Order aggregate.

Is there a way round this? Or am I missing the right way of doing this?

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:12

github_iconTop GitHub Comments

12reactions
dathwaycommented, Dec 31, 2020

Why is there no method for removing OrderItems on the Order Aggregate? Rather than addressing the problem in a blog post, how about implementing this into the app, to show folks the proper way of removing a collection from an aggregate and demonstrate it how persists that change all the way down to the DB?

9reactions
CESARDELATORREcommented, Oct 30, 2018

Good question. 👍 So, you are right in the Aggregate’s implementation. You’d implement a method called RemoveOrderItem in the Order AggregateRoot where you’d remove the item from the Order’s Item list, so in that method you are encapsulating and you could also implement additional “in-memory” behavior in that method, tha’s why you don’t want to provide direct access to the Item’s List.

Then, to your question, the Repository you have to use is just the OrderRepository (1:1 relationship with the aggregate) and call the OrderRepository.Update(order) method.

Sure, you are “removing/deleting” an order item, but from an Aggregate perspective, that is an Update/change within the order. If you had to add/change anything else for deleting Order items, you’d need to do it within the method OrderRepository.Update(order).

You MUST NOT create repositories per child entities or tables that are “child” within an aggregate (like Order). Repositories must have a 1:1 relationship with aggregates and therefore with the aggregateRoot or Root Entity (Order in this case).

The encapsulation is maintained that way. The Order aggregate controls invariants and behavior of anything in-memory within the Aggregate. The Repository controls the infrastructure persistence for that Aggregate.

Read this chapter I wrote where all the repository implementation is explained in deeper details and why it is important to “Enforce one aggregate root per repository”:

https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design

Hope that helps. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - EF code first: How to delete a row from an entity's ...
Adding is simple, you simple call .Add(Item item) on the Collection you wish to add to. A new row is added to the...
Read more >
Why is the product of the order item a reference to the ID ...
If I use the ID of the product, I have the problem that if after I accepted the order and the description of...
Read more >
newbie question of orders/line items
You would be changing the Order. If the Order had invariants over the number of items or price it would be more complex...
Read more >
Is OrderLine an Entity or A Value Object - Sapiens Works
A customer never cancels an orderline. They cancel the whole order or a product or (if allowed) they change the quantity of an...
Read more >
Question about Progressing through the game. - GameFAQs
For Kingdom Hearts 3D: Dream Drop Distance on the 3DS, a GameFAQs message board topic titled ... Then what must I do in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found