TanvirArjel / EFCore.GenericRepository

This repository contains Generic Repository implementation for Entity Framework Core
MIT License
599 stars 89 forks source link

Usage with multiple DbContexts Closed #20 #23

Closed serdarozkan41 closed 2 years ago

HybridSolutions commented 2 years ago

Not quite sure if the AddMultipleRepository makes sense because you are forcing to specify multiple DBContexts on startup class and then you still have to specify them each time on injection like you did on Employees controller class. I understand the point is to keep it prettier to code and read but it's just another place to update.

I believe that usinga more generic way like services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); is much direct since you don't have to specify anything in the future on startup.

I guess both can co-exist but for me, less dependencies, the better.

serdarozkan41 commented 2 years ago

I agree with you about minimal dependency. As an a alternative , I added the Add Multiple Repository method.

With this update I will break the work in previous versions, I need to fix this :)

HybridSolutions commented 2 years ago

I agree with you about minimal dependency. As an a alternative , I added the Add Multiple Repository method.

With this update I will break the work in previous versions, I need to fix this :)

I tested your previous code and it works great with both solutions:

// Option 1
  services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); 

  // Option 2
  //services.AddMultipleRepository(ServiceLifetime.Scoped)
  //    .AddGenericRepository<ApplicationDbContext>()
  //    .AddGenericRepository<TenantAdminDbContext>();

using it across multiple service classes without changing anything independently of the used option

public UserService(
    IRepository<ApplicationDbContext> repository,
    ILogger<User> logger)
    {      
        _repository = repository;
        _logger = logger;        
    }

Your are trying to do what now?

serdarozkan41 commented 2 years ago

I added a few changes so that the codes that were in use as IRepository _repository in the old version work without errors.

https://github.com/TanvirArjel/EFCore.GenericRepository/pull/24

HybridSolutions commented 2 years ago

I added a few changes so that the codes that were in use as IRepository _repository in the old version work without errors.

24

This example

services.AddGenericRepository<DemoDbContext>(); // default
services.AddGenericMultipleRepository<DemoDbContext2>(); // if you want multiple

is not very clear on how you support multiple. Like this?

services.AddGenericMultipleRepository<DemoDbContext1>();
services.AddGenericMultipleRepository<DemoDbContext2>();
services.AddGenericMultipleRepository<DemoDbContext3>();

I used it like this with previous code

services.AddMultipleRepository(ServiceLifetime.Scoped)
    .AddGenericRepository<DemoDbContext1>()
    .AddGenericRepository<DemoDbContext1>();
   .AddGenericRepository<DemoDbContext3>();
serdarozkan41 commented 2 years ago

The way it is used can be changed, the important thing is that it supports the previous version.

My main goal is to provide multiple support. For those who update from previous nuget versions for backward compatibility. They do not need to modify the IRepository objects they are actively using.

services.AddGenericMultipleRepository<DemoDbContext1>()
.AddGenericMultipleRepository<DemoDbContext2>()
.AddGenericMultipleRepository<DemoDbContext3>();