4
votes

My application is broken down into several assemblies.

The MyProject.Core assembly contains all of the Domain objects such as Person and Sale as well as interfaces repositories such as IPersonRepository and ISaleRepository.

The MyProject.Data is supposed to deal with persistence, but I'm a little confused about how this is supposed to work. Am I supposed to be using Entity Framework to map my tables to the domain models? Should I do this with the Fluent API? Or am I supposed to be instantiating the model classes and populating them manually?

3
"I'm a little confused about how this is supposed to work. Am I supposed to be using Entity Framework". DDD does not prescribe how things are "supposed" to work at this level. All 3 of the approaches you identified are valid. I suspect you are asking which of those approaches is the best. The only real answer to that is "it depends". - MattDavey

3 Answers

4
votes

IMO you should not try to use your domain model objects as entity framework entities. You will not be able to craft properly encapsulated domain objects consisting of atomic methods. Instead they would need to have public properties with getters and setters that EF require, and this leads to an Anemic Domain Model.

Essentially: if you try to double up your domain objects as entity framework entities, you will compromise the design.

Instead, I employ the memento pattern, whereby I re-hydrate my domain objects with EF entities that serve as the mementos.

Given that EF can just use plain POCO's I'd put these classes in a different assembly to the assembly hosting your DbContext/Respositories, as your model will need to reference them. Because they are just POCO's you won't be tying your model to EF.

So you'd potentially have three assemblies:

  • MyProject.Model ... which contains your DDD model classes
  • MyProject.Data ... which contains DBContext and Repositories
  • MyProject.Mementos ... which contains your EF POCO's

Example:

public class PersonRepository  : EntityFrameworkRepository, IPersonRepository
{
      public Person GetById(Guid personId)
      {
           using (MyDbContext ctx = new MyDbContext())
           { 
               var personMemento = (from p in ctx.People
                                   where p.PersonId == personId
                                   select p).FirstOrDefault(); 


               return Person.RestoreFromMemento(personMemento);

           }
      }
}
1
votes

The MyProject.Data is supposed to deal with persistence, but I'm a little confused about how this is supposed to work. Am I supposed to be using Entity Framework to map my tables to the domain models? Should I do this with the Fluent API? Or am I supposed to be instantiating the model classes and populating them manually?

Use what you know, or if allowed, what you want to learn.

Either way you should not leak any DB specific information back to the domain model or put any DB restrictions on the model.

0
votes

The industry standard practice is to use some sort of Object/Relation Mapper such as Entity Framework or NHibernate etc to load and persist you entities.

O/RMs are tools for bridging the Impedance Mismatch between Object Oriented laguages and Relation Databases.

They are often combined with the Repository Pattern.

It is not essential to use these tool, you could manually save your entities to a database, or possibly build you own O/RM, but will will be creating yourself many unnecessary headaches. By using an O/RM tool, you will be gaining the benefit of many others experience in building DDD applications.

It is also worth poiting out that O/RM's are not only for DDD applications.

In my applications, the Data layer is usually a few repositories, the NHibernate mapping files and any other NHibernate implementation code.