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:
services.AddScoped(typeof(IRepository<>), ...)
I am getting error Using the generic type 'IRepostiory<TIdType,TEntityType>' requires 2 type arguments
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.
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.
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.
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.
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>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With