I want to use domain-driven design in a new project. My first step was to eliminate model classes used exclusively by DAL project (Entity Framework Core) and instead use business models directly.
But during this step I encountered a problem of circular dependencies and where to put DbContext class.
Suppose I have two projects - business and data. Business project contains all the business logic and relevant entity models. Data project contains Entity Framework specific stuff - configurations, migrations and DbContext.
Problem comes from dependencies. Data project depends on business project for entity models (used by configurations and DbContext). Business project depends on data project for DbContext, which is used by the business logic. This project architecture creates a dependency cycle, which I'm not sure how to break.
I have thought of a couple (bad) solutions:
Move DbContext to business project
- This would remove data project dependency from business project, but it also pollutes business project with persistence layer stuff.
Abstract DbContext with interface located in business project.
- This would also remove data project dependency from business project. This interface would firstly need to expose all the DbSets of models persisted by the database, and secondly it would need to expose all the DbContext properties and methods used by business logic (at minimum SaveChangesAsync, possibly some other stuff). DbContext extension methods would also not work when using the interface.
How do you solve this dependency cycle when using this style of domain-drive design with Entity Framework?