Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net-core Dependency Injection

I have a Generic repository which I want to register for DI, it implements an interface IRepository.

Normally I would create an instance of it like this:

IRepository repo = new Repository<Order>();

However I am trying to get up to speed in .net 5 ahead of release and want to get this working with DI, I have resorted to the following :

services.AddTransient<DAL.IRepository<Models.Order>, DAL.Repository<Models.Order>>();

But this feels wrong, I don't want 50+ lines in there one for each of the classes in my model...

I cannot find anything online about this, I know its possible with other ioc containers.. but as this is a learning project I dont want to use another container, Im aiming to do it all with .net5s native container.

like image 657
D3vy Avatar asked Feb 11 '16 14:02

D3vy


1 Answers

You should be able to register the open generic with

services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
like image 119
qujck Avatar answered Oct 05 '22 11:10

qujck