Published on

Domain-Driven Design in Practice course source using .Net 6 with EF Core

Reading time
3 min read

Introduction

I have been refreshing my Domain-Driven Design knowledge with Domain-Driven Design learning path on Pluralsight. One of the courses available is Domain-Driven Design in Practice by Vladimir Khorikov

By the time of writing this, the course was last updated on 16 Sep 2019. The original source code is in .Net 4.5 and uses NHibernate v4 for data persistence.

As part of my practice to better understand the topic. I followed the demos by rebuilding the source code with .Net 6 and EF Core instead of NHibernate.

EF Core Notes

With EF Core. I used (what I thought) mapping techniques to represent aggregate relations in domain model. Also used the same to represent ValueObjects.

This mapping basically uses OwnsOne and OwnsMany entity configuration mapping methods to define aggregates and value objects in the model. According to EF Core documentation:

Owned entities are essentially a part of the owner and cannot exist without it, they are conceptually similar to aggregates. This means that the owned entity is by definition on the dependent side of the relationship with the owner.

For dispatching domain event part. NHibernate supports event listeners for different events like PostCommitUpdateEventListeners & PostCommitInsertEventListeners. This is also can be managed in EF Core but in different ways, like:

It's not as straight forward as it's in NHibernate. But it achieved the same purpose. However I didn't include tests around this.

Solution structure

I implemented different solution structure than the original sample solution. My solution is structured as the following

  • Domain project: For all domain entities with their business logic. It is netstandard2.1 project
  • Data project: For data access (DbContext & repositories implementations) & migrations (EF Core migrations) infrastructure.
  • WpfClient project: For UI using WPF and MVVM.
  • Domain.Tests project: For Domain unit tests using xUnit

I used Visual Studio 2022 with .Net6. Domain project is using netstandard2.1.

Running the solution

  1. Update connection string
  2. You need to set up the database first using EF Core
  3. Run the solution with WpfClient project as your startup project.

Connection string is hardcoded in:

  • DddInPractice.Data/SqlServerDesignTimeDataContextFactory project
  • DddInPractice.WpfClient/App.xaml.cs

To run database migrations. You need to have EF Core tools installed. Use this .Net CLI command.

Check If you have EF Core command-line tools:
dotnet ef

Install EF Core command-line tools:
dotnet tool install --global dotnet-ef

Run Db Migrations for the solution:
dotnet ef database update --project src/DddInPractice.Data --startup-project src/DddInPractice.Data
Note that the above command assumes you are running the command from within the solution folder.

Source

Thank you

I highly recommend the course and the whole learning path if you are interested in the subject of Domain-Driven Design.

And if you are interested in the the updated source code you can find it here.