Comments (2)
Hi @jmalczak,
There are many ways to implement this kind of behavior, all of them have pros and cons:
-
Treat Customer and his Orders as a whole - the Aggregate Pattern. So you load all orders together with the Customer and protect this invariant ("you cannot remove Customer with Orders"). The main advantage is that you will have strong consistency (because Aggregate is a consistency boundary). The main disadvantage is performance - you need load and save always the whole Aggregate.
-
Load data about Orders in Application Service and check this logic. Checking this logic can be done in Application Service or in the Aggregate Root method. The main advantage is that you don't need to always load all orders together with the Customer entity - only during removing use case. The drawback is that between loading data and performing the action (removal) there is a small amount of time and in that time the state of the system could change (for example first order was placed). So there is a "small" chance that this rule will be broken.
-
Use database constraints. Yes, your business logic should not be in the database, but sometimes it is unavoidable or it is the easiest solution. So you can use FK constraint (if you performing hard delete) or even the trigger (yes, evil) if you performing soft delete (using some kind of status or flag). Advantage - strong consistency, disadvantage - business logic in the database, harder to test, maintain, less readable etc.
-
Similar approach to what you described. You can have a flag on the Customer called "CanBeDeleted" with default value "true" and after adding new Order it can be set to false. Advantage - performance, because you have done some aggregation/precalculation. Disadvantage - you need to keep this flag up to date.
Back to your question about communication between context. If you need strong consistency you need to have Customer and his Orders in the same context. If you don't need strong consistency you can apply approach nr. 2 (ask other context) or communicate by events and store information in Customer context (approach nr 4.)
from sample-dotnet-core-cqrs-api.
Great answer, thanks for the explanation!
from sample-dotnet-core-cqrs-api.
Related Issues (20)
- Query Handlers and responsabilities HOT 3
- "Light" OrderDto HOT 1
- If there have multiple instances of ProcessOutBoxJob, The events may be processed in many times. HOT 3
- Should we persist this to the database? HOT 2
- Migrations HOT 1
- Problem with context.SaveChangesAsync() after removing objects from List<>
- StronglyTypedIdValueConverterSelector should be Singleton HOT 1
- Product existence check / Asynchronous domain HOT 1
- Outbox pattern maybe lock in OutboxMessages table
- Is it mandatory to use Guid for Id in entity or aggregate? HOT 1
- 1
- Decorator registration is not working with Autofac 6.x HOT 1
- Raw SQL in application layer
- DDD EF Core Remove issue HOT 8
- IDomainEventDispatcher and its implementation class
- Customer as Aggregate
- Ordering of DomainEventNotifications in OutboxMessages table
- Add a license HOT 1
- why not just inject IDbConnection instead of ISqlConnectionFactory? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sample-dotnet-core-cqrs-api.