天天看點

asp.net core 全局注入

直接上 代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Runtime.Loader;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.DependencyModel;

using Microsoft.Extensions.Logging;

using Microsoft.Extensions.Options;

using DotNetCore.Entities;

using DotNetCore.Repository.Interfaces;

using DotNetCore.Repository;

namespace DotNetCoreDapper

{

    public class Startup

    {

        public Startup(IConfiguration configuration)

        {

            Configuration = configuration;

        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.

        public void ConfigureServices(IServiceCollection services)

        {

            services.AddOptions();

            services.Configure<Option>(Configuration.GetSection("Option"));

            #region 注冊

            //全局注冊

            foreach (var itemClass in GetClassInterfacePairs("DotNetCore.Business"))

            {

                foreach (var itemInterface in itemClass.Value)

                {

                    services.AddTransient(itemInterface, itemClass.Key);

                }

            }

            foreach (var itemClass in GetClassInterfacePairs("DotNetCore.Repository"))

            {

                foreach (var itemInterface in itemClass.Value)

                {

                    services.AddTransient(itemInterface, itemClass.Key);

                }

            }

            //注冊泛型

            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));    

            #endregion

            services.AddMvc();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            app.UseMvc();

        }

        #region 輔助類

        private Dictionary<Type, List<Type>> GetClassInterfacePairs(string assemblyName)

        {

            //存儲 實作類 以及 對應接口

            Dictionary<Type, List<Type>> dic = new Dictionary<Type, List<Type>>();

            Assembly assembly = GetAssembly(assemblyName);

            Type[] types = assembly.GetTypes();

            foreach (var item in types.AsEnumerable().Where(x => !x.IsAbstract && !x.IsInterface && !x.IsGenericType))

            {

                dic.Add(item, item.GetInterfaces().Where(x => !x.IsGenericType).ToList());

            }

            return dic;

        }

        private List<Assembly> GetAllAssemblies()

        {

            var list = new List<Assembly>();

            var deps = DependencyContext.Default;

            var libs = deps.CompileLibraries.Where(lib => !lib.Serviceable && lib.Type != "package");//排除所有的系統程式集、Nuget下載下傳包

            foreach (var lib in libs)

            {

                try

                {

                    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));

                    list.Add(assembly);

                }

                catch (Exception)

                {

                    // ignored

                }

            }

            return list;

        }

        private Assembly GetAssembly(string assemblyName)

        {

            return GetAllAssemblies().FirstOrDefault(assembly => assembly.FullName.Contains(assemblyName));

        }

        #endregion

    }

}

github 上 也在 持續更新  https://github.com/AlbertBJ/DotNetCoreDapper

知乎: https://zhuanlan.zhihu.com/albertwang

微信公衆号:AI-Research-Studio

asp.net core 全局注入

​​

下面是贊賞碼

asp.net core 全局注入

繼續閱讀