c# - .net-core 依赖注入(inject)

标签 c# dependency-injection inversion-of-control asp.net-core

我有一个通用存储库,我想为 DI 注册它,它实现了一个接口(interface) IRepository。

通常我会像这样创建一个实例:

IRepository repo = new Repository<Order>();

然而,我正试图在发布之前加快 .net 5 的速度,并希望它与 DI 一起工作,我已经采取了以下措施:

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

但这感觉不对,我不希望模型中的每个类都有 50 多行......

我在网上找不到任何关于此的信息,我知道它可能与其他 ioc 容器一起使用。但由于这是一个学习项目,我不想使用其他容器,我的目标是使用 .net5s native 容器来完成这一切。

最佳答案

你应该能够注册开放泛型

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

关于c# - .net-core 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342472/

相关文章:

multithreading - Simpleinjector : Is this the right way to RegisterManyForOpenGeneric when I have 2 implementations and want to pick one?

angular - 在 Angular 6 中,我在组件中收到错误 "NullInjectorError: No provider for Store"

c# - .NET 中的 IoC 是什么

Linux 和 Mac 中的 C#

c# - WKHTMLTOPDF 不呈现 Base64 图像

c# - 我知道如何使用依赖注入(inject),但我认为它没有实际优势

entity-framework - 每次使用都需要创建/处理的项目的依赖注入(inject)

java - 延迟初始化在 Spring MVC 3 中不起作用

c# - UDP 打洞帮助

c# - 使用哪个 C# 多线程选项