zoukankan      html  css  js  c++  java
  • .net core实现批量依赖注入

      最近在做公司老项目升级,要将原有的.net framework程序,升级到.net core平台,这个过程中就发现了一个问题,老项目在调用Service或者是Repository的时候都是直接new出来的,这显然不符合我们.net core的规范,在.net core里边更推荐用依赖注入,这就犯难了,如果我每个Service或者是Repository都在Starpup里面注册,怕是要写个几百行,而且也很繁琐。

      今天上午,想了想要不求助一下大佬 @7thiny ,经过大佬的帮助,写了一个用于全局注册的方法,代码如下:

    public static class InjectHelper
        {
            /// <summary>
            /// Add Scoped from InterfaceAssembly and ImplementAssembly to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
            /// </summary>
            /// <param name="services"></param>
            /// <param name="interfaceAssembly"></param>
            /// <param name="implementAssembly"></param>
            public static void AddScoped(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
            {
                var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
                var implements = implementAssembly.GetTypes();
                foreach (var item in interfaces)
                {
                    var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                    if (type != null)
                    {
                        services.AddScoped(item, type);
                    }
                }
            }
    
            /// <summary>
            /// Add AddSingleton from InterfaceAssembly and ImplementAssembly to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
            /// </summary>
            /// <param name="services"></param>
            /// <param name="interfaceAssembly"></param>
            /// <param name="implementAssembly"></param>
            public static void AddSingleton(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
            {
                var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
                var implements = implementAssembly.GetTypes();
                foreach (var item in interfaces)
                {
                    var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                    if (type != null)
                    {
                        services.AddSingleton(item, type);
                    }
                }
            }
    
            /// <summary>
            /// Add AddTransient from InterfaceAssembly and ImplementAssembly to the specified Microsoft.Extensions.DependencyInjection.IServiceCollection.
            /// </summary>
            /// <param name="services"></param>
            /// <param name="interfaceAssembly"></param>
            /// <param name="implementAssembly"></param>
            public static void AddTransient(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
            {
                var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
                var implements = implementAssembly.GetTypes();
                foreach (var item in interfaces)
                {
                    var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                    if (type != null)
                    {
                        services.AddTransient(item, type);
                    }
                }
            }
        }

      这样我们就可以通过丢两个Assembly进去,来帮助我们批量注册服务,在Startup中调用方法如下:

                services.AddScoped(Assembly.Load("xxxx.IService"), Assembly.Load("xxxx.Service"));
                services.AddScoped(Assembly.Load("xxxx.IService"), Assembly.Load("xxxx.Service"));
  • 相关阅读:
    界面控件DevExpress WPF入门 表达式编辑器功能
    Telerik UI for WPF全新版本——拥有Office2019高对比度主题
    DevExpress报表控件v21.2 全新的Visual Studio报表设计器
    报告生成器FastReport .NET入门指南 在Linux中启动应用程序
    文档控件DevExpress Office File API v21.2 自定义字体加载引擎
    UI组件库Kendo UI for Angular入门 如何开始使用图表功能
    WPF界面工具Telerik UI for WPF入门级教程 设置一个主题(二)
    DevExtreme初级入门教程(React篇) TypeScript支持
    报表开发利器FastReport .NET v2022.1 添加关键对象和属性
    python项目打包(一) setup.py、Python源代码项目结构
  • 原文地址:https://www.cnblogs.com/Yuuuuu/p/12017180.html
Copyright © 2011-2022 走看看