Why is the infrastructure layer using the domain objects from the core layer as models for DbSets in the DbContext? Doesnt that defeat the whole purpose? #716
-
Hi Im new to DDD in general but seems to me like my SQL tables will be mapped 1:1 to my domain? I thought DDD meant that while creating the domain I wouldnt need to worry about how its persisted. For example I want to merge some properties of the domain object into a single string column in my DB but using the domain object directly in the DbSet just means the whole model will be created as it is in the SQL database. Am I missing something here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
"I thought DDD meant that while creating the domain, I wouldnt need to worry about how its persisted." I would say this is half true. Knowing how to persist is still important for the infrastructure project. You are free to save this however you want. but you will need to get to and from your domain entities. The value statement of DDD is that changes to how you save your data should not effect the structure of your core domain enities. Core should not know how your data is saved. (but should know the interface that allows saving and retrieving data.) |
Beta Was this translation helpful? Give feedback.
-
I agree with @lukehammer. Why do we directly leverage the domain model classes from Core via EF DbContexts in Infrastructure? Because we can, with no ill effects! EF Code First is a feature they added many years ago specifically because developers wanted this capability. Prior to that, one would have a DbContext which had its own models, and you would need to map them manually to your domain model, and it was just one more set of mappings that didn't really add any value. Now, if you don't own your database, which means you don't have a DDD bounded context, then you may find value in having a separate persistence model in your infrastructure code. Why? Because you want your domain model to be able to evolve as you learn more about the business, but you're forced to use an existing database schema/model. So this is a scenario in which it might make sense to have separate persistence and domain models. But it's only because it is not the ideal case. It's a workaround. If you're following DDD, then within your bounded context you have a ubiquitous language which dictates how your model is organized and named, and that language should flow through to your persistence model. You decide to change HTH. |
Beta Was this translation helpful? Give feedback.
I agree with @lukehammer.
Why do we directly leverage the domain model classes from Core via EF DbContexts in Infrastructure? Because we can, with no ill effects! EF Code First is a feature they added many years ago specifically because developers wanted this capability. Prior to that, one would have a DbContext which had its own models, and you would need to map them manually to your domain model, and it was just one more set of mappings that didn't really add any value.
Now, if you don't own your database, which means you don't have a DDD bounded context, then you may find value in having a separate persistence model in your infrastructure code. Why? Because you want your domain model t…