Home > OS >  How to separate my Aggregates by using EF DbContext in DDD
How to separate my Aggregates by using EF DbContext in DDD

Time:11-23

I have 5 domains and i need to separate each domain with different database,so how can I do that separation using Entity Framework.

CodePudding user response:

Domain driven design has no opinion on your persistence mechanism such as how you design or structure your database or databases. So from a DDD perspective, the answer is "however you like"!

But, if you would like to separate your domains into different databases (even though you don't have to) your options are:

  1. Keep everything across all your domains in one DbContext and pass interfaces into your classes that give access to only particular domain classes, such as IOrdersRepository that accesses Orders from the DbContext, and an IInvoicesRepository that accesses only Invoices from the same DbContext.
  2. Add multiple DbContexts to your app for each domain, like SalesDbContext, BillingDbContext, MarketingDbContext and give each DbContext its own connection string in your Startup.cs file.
  3. Split your app into smaller projects (like microservices) like a Sales.Api with its own DbContext, and a Billing.Api with its own DbContext.

There may be other options I haven't thought of too, seeing as DDD doesn't prescribe anything about how you store your data.

  • Related