I have a web application being built using DDD and Onion architecture in .NET Core 5.
There are 4 layers:
- Domain layer - which contains domain models
- Application layer - which has core business logic and have reference to domain layer
- Infrastructure layer - which uses EF Core to retrieve and save data to Azure SQL, and it refers to the application layer
- API layer - which has controllers and refers to application and infrastructure layers
Reference to infrastructure layer via API layer is only to connect to the database and any request to database will go via application layer; the application layer has interfaces that are implemented in the infrastructure layer to save and retrieve data from the database.
Now, when I create database using a code-first or database-first approach, where should these EF model classes should go?
Placing them in the domain layer will break the onion architecture, as the domain layer should not know anything about the infrastructure layer.
- Placing models and mappings in Domain layer
I have seen couple of examples where models and mappings are placed in domain model and those mappings are used in infrastructure layer OnModelCreating(..)
, but still the infrastructure layer mappings which are database-specific are present in the domain layer. What if I want to replace the infrastructure layer with some other database, in which case I need to change the domain model which seems not right to me..
- Placing models in infrastructure layer
I can place EF model classes in the infrastructure layer, but how do I reference them in the application layer? Do I need to have an interface for each model repository or is there any multiple dependency injection for same interface?
I want domain model to hold by business models and place EF models in infrastructure layer and use AutoMapper to map between them if necessary.
So what is the best practice to hold EF models so it won't break Onion architecture?