Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean architecture with EF Core DB First approach #44

Open
milyas-salik opened this issue Jan 26, 2022 · 3 comments
Open

Clean architecture with EF Core DB First approach #44

milyas-salik opened this issue Jan 26, 2022 · 3 comments

Comments

@milyas-salik
Copy link

Hi,

I am implementing clean architecture using the existing database, with scaffolding command I have generated the POCO entities in the Infrastructure layer and as well as manually created the entities in the domain layer to map them later.

in the Application layer, I have the generic interface repository with a few standard operations.

public interface IRepository<T> where T : class
 {
     Task<IReadOnlyList<T>> GetAllAsync();
     Task<T> GetByIdAsync(int id);
     Task<T> AddAsync(T entity);
     Task UpdateAsync(T entity);
     Task DeleteAsync(T entity);
 }

As per the principles of Clean-Architecture, I am implementing it in the Infrastructure layer.

public class Repository<T> : IRepository<T> where T : class
    {

        protected readonly MyDBContext _MyDBContext;
        public Repository( MyDBContext mydbContext)
        {
            _MyDBContext= mydbContext;
        }
        public async Task<T> AddAsync(T entity)
        {
            await _MyDBContext.Set<T>().AddAsync(entity);
            await _MyDBContext.SaveChangesAsync();
            return entity;
        }

-----
----

As per the template, I am using a Mediator pattern with CQRS, when I try to save the user from the API layer I will end up with the below exception.

System.InvalidOperationException: Cannot create a DbSet for 'ABC.Domain.Entities.User' because this type is not included in the model for the context. However, the model contains an entity type with the same name in a different namespace: 'ABC.Infrastructure.Models.User'.

It will be resolved if I can able to map the domain entity to the infrastructure entity in the above Repository implementation.
In the above implementation, the T is the ABC.Domain.Entities.User, not the ABC.Infrastructure.Models.User. I can't pass the ABC.Infrastructure.Models.User from the Application layer ( because I can't add a reference to the Infrastructure layer in Application layer) due to the rule Clean Architecture all dependencies flow inwards and Core has no dependency on any other layer.

Please help me to map the incoming domain entity with the infrastructure entity in the above repository implementation so that I can use these general methods for other entity operations as well.

@iayti .. Please do the needful.

@iayti
Copy link
Owner

iayti commented Jan 28, 2022

Hi @milyas-salik ,

I didn't have enough time to test what you wrote. Can you try updating the Models folder to look at the Entities folder in the Domain project while doing Database Scaffolding? Db first runs as database-dependent at startup. In this example, we are trying to make it database-independent.

If you have time, could you create an example project with the steps you followed and share it with me? Let's try to solve the error together.

@milyas-salik
Copy link
Author

milyas-salik commented Jan 29, 2022

@iayti : here is my source code.
https://gitlab.com/mail2mdilyas/workcontinent/-/tree/master

The one way, I am thinking is to get rid of the dependency with infrastructure entities (generated from the database using scaffolding) and use the domain entities with the model builder ==> modelBuilder.Entity - Professional class should be the one coming from your domain instead of infrastructure

As per me, this is the easier solution for DB's first approaches using clean architecture. I need your advice on it, is it a valid adjustment or do you advise using any other alternative solution? At the same time, I don't want to break the clean architecture rules as I want scalable and maintainable applications in the future.

@wahabalemi
Copy link

wahabalemi commented Sep 17, 2023

hi when use " dotnet ef migrations add "CreateDb" --project src\Common\CleanArchitecture.Infrastructure.SqlServe --startup-project src\Apps\CleanArchitecture.Api " command cli returns "The entity type 'DomainEvent' requires a primary key to be defined."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants