A lot of tutorials on DDD I studied are mostly covering theory. They all have rudimentary code examples (Pluralsight and similar).
On the web there are also attempts by a few people to create tutorials covering DDD with EF.
If you begin studying them just briefly - you quickly notice they differ a lot from one another. Some people recommend to keep the app minimal and to avoid introducing additional layers e.g. repository on top of EF, others are decidedly generating extra layers, often even violating SRP by injecting DbContext
into Aggregate Roots.
I'm terribly apologizing if I'm asking an opinion-based question, but...
When it comes to practice - Entity Framework is one of the most powerful and widely-used ORMs. You will not find a comprehensive course covering DDD with it, unfortunately.
Important aspects:
Entity Framework brings UoW & Repository (
DbSet
) out of the boxwith EF your models have navigation properties
with EF all of the models are always available off
DbContext
(they are represented as aDbSet
)
Pitfalls:
you cannot guarantee your child models are only affected via Aggregate Root - your models have navigation properties and it's possible to modify them and call
dbContext.SaveChanges()
with
DbContext
you can access your every model, thus circumventing Aggregate Rootyou can restrict access to the root object's children via
ModelBuilder
inOnModelCreating
method by marking them as fields - I still don't believe it's the right way to go about DDD plus it's hard to evaluate what kind of adventures this may lead to in future (quite skeptical)
Conflicts:
without implementing another layer of repository which returns Aggregate we cannot even partly resolve the abovementioned pitfalls
by implementing an extra layer of repository we are ignoring the built-in features of EF (every
DbSet
is already a repo) and over-complicating the app
My conclusion:
Please pardon my ignorance, but based on the above info - it's either Entity Framework isn't adequate for Domain-Driven Design or the Domain-Driven Design is an imperfect and obsolete approach.
I suspect each of the approaches has its merits, but I'm completely lost now and don't have the slightest idea of how to reconcile EF with DDD.
If I'm wrong - could anyone at least detail a simple set of instructions (or even provide decent code examples) of how to go about DDD with EF, please?