Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

c# - .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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should be able to register the open generic with

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...