Let's do some DDD with Entity Framework Core 3!
Tags: ddd, entityframework, dotnet
Published: 2019-10-10 19:13:04

<!-- wp:paragraph -->
<p>Few days ago Microsoft released <strong>Entity Framework Core 3</strong>, introducing a <a rel="noreferrer noopener" aria-label="lot of improvements (opens in a new tab)" href="https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/" target="_blank">lot of improvements</a> in both functionalities and performance ( and some breaking change as well).</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Now we have full support for very cool stuff like C# 8, Async Enumerables, plus a brand new LINQ provider.</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I'm using a lot  <strong>Entity Framework</strong> in my daily job but of course before upgrading a project 's dependency to a mayor version there's always some testing to do.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As I often do, I decided to write a small demo and try out some of these new functionalities. You can find the all sources <a rel="noreferrer noopener" aria-label="available on GitHub (opens in a new tab)" href="https://github.com/mizrael/EFCoreCommerceDemo" target="_blank">available on GitHub</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>My goal for this project was to model a bunch of <a href="https://lostechies.com/jimmybogard/2008/05/21/entities-value-objects-aggregates-and-roots/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Aggregates and Value Objects</a> with proper <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://deviq.com/persistence-ignorance/" target="_blank">Persistence Ignorance</a>, eg: I don't want to pollute my business logic classes with code responsible of storing and retrieving data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Luckily <strong>Entity Framework</strong> <strong>Core</strong> offers the possibility to configure the Entity/Table mapping using a nice <a href="https://martinfowler.com/bliki/FluentInterface.html" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Fluent Interface</a> , avoiding attributes on our properties.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We have basically two options. First one is to write all the configuration code in the <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.onmodelcreating?view=efcore-2.1#Microsoft_EntityFrameworkCore_DbContext_OnModelCreating_Microsoft_EntityFrameworkCore_ModelBuilder_" target="_blank" rel="noreferrer noopener" aria-label="OnModelCreating() (opens in a new tab)">OnModelCreating()</a> method of our DbContext. Quick and easy, but leads to a lot of very confused code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A much cleaner option is to use the <a rel="noreferrer noopener" aria-label="IEntityTypeConfiguration<> (opens in a new tab)" href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.ientitytypeconfiguration-1?view=efcore-2.1" target="_blank">IEntityTypeConfiguration&lt;&gt;</a> interface.  With it we can separate the configuration for each Entity and have a much cleaner structure.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Using this second option, I modeled a very simple eCommerce scenario with Products, Quotes and Orders. These last two Entities also hold one-to-many relationships with Quote Items and Order Lines respectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When you run the application the first time, it will generate the db, which looks like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":6801,"align":"wide","linkDestination":"media"} -->
<figure class="wp-block-image alignwide"><a href="https://www.davideguida.com/wp-content/uploads/2019/10/image-2.png" target="_blank" rel="noreferrer noopener"><img src="https://www.davideguida.com/wp-content/uploads/2019/10/image-2.png" alt="" class="wp-image-6801"/></a><figcaption>database diagram generated with  <a href="https://sqldbm.com/">https://sqldbm.com</a> </figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Then the code will:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>create some products</li><li>add just one product to a quote and save it</li><li>update the quote adding another product</li><li>create an order from that quote</li><li>add another product to the quote</li><li>create another order from the quote</li></ol>
<!-- /wp:list -->

<!-- wp:image {"id":6797,"align":"wide","linkDestination":"media"} -->
<figure class="wp-block-image alignwide"><a href="https://www.davideguida.com/wp-content/uploads/2019/10/image.png" target="_blank" rel="noreferrer noopener"><img src="https://www.davideguida.com/wp-content/uploads/2019/10/image.png" alt="" class="wp-image-6797"/></a></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In another post we're going to take a look at the code and talk about each Entity configuration. Stay tuned!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><i><small>this article is available also on my blog, <a href="https://www.davideguida.com/lets-do-some-ddd-with-entity-framework-core-3/" target="_blank">check it out!</a></small></i></p>
<!-- /wp:paragraph -->