Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a generic dependency injection [duplicate]

Working on a read-only api service and making use of generics to package the operation into convention based process.

Repository interface:

public interface IRepository<TIdType,TEntityType> where TEntityType:class {
   Task<EntityMetadata<TIdType>> GetMetaAsync();
}

Repository implementation:

public class Repository<TIdType,TEntityType> : IRepository<TIdType,TEntityType> where TEntityType:class {
   public Repository(string connectionString) { // initialization }
   public async Tas<EntityMetadata<TIdType>> GetMetaAsync() { // implementation   }
}

In Startup.cs -> ConfigureServices :

services.AddSingleton<IRepository<int, Employee>> ( p=> new Repository<int, Employee>(connectionString));
services.AddSingleton<IRepository<int, Department>> ( p=> new Repository<int, Department>(connectionString));
// and so on

Controller:

public class EmployeeController : Controller {
   public EmployeeController(IRepository<int,Employee> repo) {//stuff}
}

I am currently repeating the repository implmentation for all types of entity types in the ConfigureServices. Is there a way to make this generic too?

services.AddSingleton<IRepository<TIdType, TEntityType>> ( p=> new Repository<TIdType, TEntityType>(connectionString));

so in the controller constructor call can automatically get the relevant repository?

Update 1: Not a duplicate:

  1. The repository implementation does not have default constructor
  2. Because it does not have default constructor, I cannot provide the solution given in the linked question.
  3. When trying services.AddScoped(typeof(IRepository<>), ...) I am getting error Using the generic type 'IRepostiory<TIdType,TEntityType>' requires 2 type arguments
like image 256
Vijay Avatar asked Mar 29 '17 08:03

Vijay


People also ask

How do I register a generic repository in .NET Core?

You have to register the dependencies in IServiceCollection in startup class. ASP.NET Core supports the constructor Injections to resolve the injected dependencies. Here, register the interface and their implementation into DI, using the add method of different lifetimes.

CAN interface have multiple implementations C#?

Using the delegate func Introduce three implementation classes as below- three different classes where we have implemented the same interface. ASP.NET Core has built-in support for dependency injection. However, multiple implementations of an interface in ASP.NET Core is tricky.

How does dependency injection Work in c#?

Dependency Injection is done by supplying the DEPENDENCY through the class's constructor when creating the instance of that class. The injected component can be used anywhere within the class. Recommended to use when the injected dependency, you are using across the class methods.

Why to use dependency injection c#?

Dependency Injection (or inversion) is basically providing the objects that an object needs, instead of having it construct the objects themselves. It is a useful technique that makes testing easier, as it allows you to mock the dependencies.


1 Answers

Since this question is still not properly marked as duplicate: The way to register a Generic class:

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

now you can resolve it in the following way:

serviceProvider.GetService(typeof(IRepository<A,B>));
// or: with extensionmethod
serviceProvider.GetService<IRepository<A,B>>();
like image 85
Joel Harkes Avatar answered Oct 23 '22 00:10

Joel Harkes