.NET的核心依赖注入 [英] .net-core Dependency Injection

查看:138
本文介绍了.NET的核心依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要为DI注册一个通用存储库,它实现了一个接口IRepository

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>();



不过我想起床的速度在.NET 5领先发布,并希望得到这与DI工作,我已经使出了以下内容:

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>>();



不过,这种感觉错了,我不想在那里1 50+线每个班在我的模型......

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

我不能在网上找到任何有关这一点,我知道它有可能与其他IOC容器..但因为这是一个学习项目,我不想用另一个容器而言,我的目标是什么,这一切以.net5s机容器。

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.

推荐答案

在一些回去和向前的意见其他答案我有一个有效的解决方案,它可能不是最好的方式,但它的工作原理。 。生病更新,如果我再一次找到实现这个更好的办法

After some back and forwards in the comments to other answers I have a working solution, It might not be the best way but it works. Ill update again if I find a better way to implement this.

这两个问题我已经是:需要注册一个通用接口,这里的问题是浓度的失误我的一部分。我有错误的语法登记泛型类型这当然是:

The two issues I had were : Needed to register a generic interface, the issue here was a lapse in concentration on my part.. I had the syntax wrong for registering a generic type which of course is :

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



第二个问题是,我有一个包含50+这我想注册不同型号的大会,方式,我解决了这个是写,我可以说我想要注册的命名空间一起传递组件列表的方法,它遍历符合标准,并在DI容器注册它们。任何类型

The second issue was that I have an assembly which contains 50+ different models which I wanted registered, The way that I addressed this was to write a method that I can pass a list of assemblies to along with the Namespace that I want to register and it iterates over any types that match the criteria and registers them in the DI container.

public void RegisterModels(IServiceCollection services, string[] Assemblies, string @NameSpace)
    {
        foreach (var a in Assemblies)
        {
            Assembly loadedAss = Assembly.Load(a);

            var q = from t in loadedAss.GetTypes()
                    where t.IsClass && !t.Name.Contains("<") && t.Namespace.EndsWith(@NameSpace)
                    select t;

            foreach (var t in q.ToList())
            {
                Type.GetType(t.Name);
                services.AddTransient(Type.GetType(t.FullName), Type.GetType(t.FullName));
            }
        }
    }

这然后从所谓startup.cs方法ConfigureServices:

This is then called from the startup.cs method ConfigureServices :

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<TestContext>(options =>
                options.UseSqlServer(@"Server=LOCALHOST\SQLEXPRESS;Database=Test;Trusted_Connection=True;"));

        services.AddMvc();

        RegisterModels(services, new string[] { "UI" }, "UI.Models");

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

有可能是一个更好的方式来做到这一点,肯定是使用不同的DI容器,如果任何人有改进,以提供请让我知道。

There may be a better way to do this, there definitely is using different DI containers, if anyone has improvements to offer please let me know.

这篇关于.NET的核心依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆