Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
349 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should be able to register the open generic with

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...