Use EF with DDD principles is possible ??? RRS feed

  • Question

  • Hi all!

    I'm newbie and i have some curiosity on architecture of application :

    I'm studing DDD principles and in the past I studied how EF works,

    is possible to integrate both system to build a business modular application ?

    are there any helpful guides on their integration?

    Thanks for all suggestions

    Friday, April 5, 2019 4:53 PM


  • Yes, Code First will create the tables. Please accept the answers if I've answered your questions, that way others can benefit from it.

    Thursday, April 11, 2019 6:46 PM

All replies

  • DDD and EF have little / nothing to do with one another.

    DDD is a modeling concept and EF is an Object Relational Mapper (ORM). It is basically a persistence technology. A DDD design may use EF in some form under the hood, but the two should not interact in any other way.

    Some interpretations of Domain-Driven Design do actually advocate data-modeling. With this in mind, "Entities" and "Value Objects" are essentially function-less data holders, and the design concerns itself with what properties these hold and what relation they have between one another. 

    In conclusion: DDD and EF are not mutually exclusive, they are actually irrelevant to one another. As long as you are doing proper object-modeling and not data-modeling. DDD objects should not in any shape or form be EF artifacts. DDD Entities should not be EF "entities". Inside some business-relevant method, a DDD design could use EF with some related data-objects however, they should be hidden under a business-relevant behavior-oriented interface.

    Wednesday, April 10, 2019 1:17 PM
  • I imagined what you're telling me, that a DDD Entity is not an EF Entity, since an EF Entity is the representation of a physical table of a DB as an object, while a DDD Entity is a conceptual object made up of other objects that may or may not be EF entities.

    Now I would like to ask you, given your experience in this regard, whether to operate in this way in the OOP is a good way or there are better ones ?

    Wednesday, April 10, 2019 4:25 PM
  • To be clear, when modeling the domain, you want to forget about persistence. I wouldn't recommend modeling your domain using EF entity classes just like I wouldn't recommend modeling a domain using raw DataSet or DataTable objects. I look at EF as a raw time-saver with built-in support for the repository pattern where the Context is the Unit of Work and the DbSet being the repository. This is how I look at it.


    That being said, if you want to use EF as your domain model, then the object model targeted by EF will map to your domain model via the Code First approach. With Code First, you point to your DDD Entities in separate EF mapping files living in a non-domain layer and refrain from using EF artifacts such as data annotations directly in your Entity classes. This approach is not possible when using Database First.

    william xifaras

    Wednesday, April 10, 2019 5:59 PM
  • Is there a better alternative to EF with .NET and SQL Server ?

    I think I didn't understand how to proceed with EF/DDD :

    I create the Domain model with EF Code First

    and then I create the aggregates of entities DDD or viceversa ?

    can you make a simple example?

    see you tomorrow, I have to run away.


    Wednesday, April 10, 2019 6:16 PM
  • Wednesday, April 10, 2019 6:22 PM
  • and then I have to create other different entities for DDD ?

    Thursday, April 11, 2019 8:07 AM
  • No with code first, your entity classes are your domain classes.

    Have a look at this EF 6 code 1st sample.

    william xifaras

    Thursday, April 11, 2019 2:28 PM

  • I create my aggregate entities and then with EF Code First will be created the tables of DB

    Is this approach sufficient?

    • Edited by AngeloNew Thursday, April 11, 2019 4:18 PM
    Thursday, April 11, 2019 4:10 PM
  • Yes, Code First will create the tables. Please accept the answers if I've answered your questions, that way others can benefit from it.

    Thursday, April 11, 2019 6:46 PM
  • you have been clear, thanks
    Friday, April 12, 2019 4:07 PM