0

I've developed a generic repository pattern with EF Core (GenericRepository.cs), but want to extend this by adding it to multiple databases.

GenericRepository.cs

public class GenericRepository<TEntity, TContext> : IGenericRepository<TEntity> where TEntity : class where TContext : DbContext
{
            
    private readonly TContext _context;
    
    public GenericRepository(TContext context)
    {
        _context = context;
    }
    
    public TEntity GetById(string id)
    {
        return _context.Set<TEntity>().Find(id);
    }

    ...
}

With that in mind I then made the ContextRepository.cs generic by adding a TContext to the class. This only worked if the context class was of type DbContext.

ContextRepository.cs

public class ContextRepository<TContext> : IContextRepository where TContext : DbContext
{
    private readonly TContext _dbContext;
    private readonly Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
    public Dictionary<Type, object> Repositories
    {
        get { return _repositories; }
        set { Repositories = value; }
    }

    public ContextRepository(TContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IGenericRepository<TEntity> Repository<TEntity>() where TEntity : class
    {
        if (Repositories.Keys.Contains(typeof(TEntity)))
        {
            return Repositories[typeof(TEntity)] as IGenericRepository<TEntity>;
        }

        IGenericRepository<TEntity> repo = new GenericRepository<TEntity, TContext>(_dbContext);
        Repositories.Add(typeof(TEntity), repo);
        return repo;
    }
}

Here's the issue:

I have two different database contexts (ApiAuthorizationDbContext and DbContext).

Is there a way/workaround to achieve this with both contexts in the same generic context class?

6
  • If ApiAuthorizationDbContext is derived from DbContext then it will also work in the generic repository, new ContextRepository<ApiAuthorizationDbContext>(); Commented Aug 24, 2022 at 6:25
  • You may not be aware but you're not supposed to implement the repository pattern with EF because EF already does it. The DbContext is a unit of work and each DbSet is a repository. Commented Aug 24, 2022 at 6:27
  • @MarcusHöglund thank you for the suggestion. Will give it a go and test. Commented Aug 24, 2022 at 8:23
  • @John Thank you, I did not think about it like that. Commented Aug 24, 2022 at 8:25
  • I learned the hard way. We used the same pattern with ADO.NET in my office and we started using EF fairly early on and started by implementing our own repositories too, but it created a number of issues. Commented Aug 24, 2022 at 8:38

1 Answer 1

1

As @John stated in the comments.

You may not be aware but you're not supposed to implement the repository pattern with EF because EF already does it. The DbContext is a unit of work and each DbSet is a repository.

In my particular case this was unnecessary. I found this article explaining it in more detail for anyone wandering down the same path.

Repository Pattern with Entity Framework Core is obsolete

Developers building applications with ASP.Net Core and Entity Framework Core should not use UoW and Repository pattern anymore. EF Core supports unit testing and mock contexts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.