Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <a href="http://en.wikipedia.org/wiki/Anemic_domain_model" rel="nofollow">Anemic Domain Model</a>. </p> <p><em><strong>Essentially: if you try to double up your domain objects as entity framework entities, you will compromise the design.</em></strong></p> <p>Instead, I employ the <a href="http://en.wikipedia.org/wiki/Memento_pattern" rel="nofollow">memento pattern</a>, whereby I re-hydrate my domain objects with EF entities that serve as the mementos.</p> <p>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.</p> <p>So you'd potentially have three assemblies:</p> <ul> <li>MyProject.Model <em>... which contains your DDD model classes</em></li> <li>MyProject.Data <em>... which contains DBContext and Repositories</em></li> <li>MyProject.Mementos <em>... which contains your EF POCO's</em></li> </ul> <p>Example:</p> <pre class="lang-cs prettyprint-override"><code>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); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload