日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入

發布時間:2025/4/14 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載來源?http://www.cnblogs.com/xiaoliangge/p/7642372.html

ASP.net Core自帶DI(依賴注入),用法如下:

services.AddScoped(typeof(IProductService), typeof(ProductService));

如果服務較多,必定造成文件難以維護

所以需要利用反射批量實現注冊

核心代碼如下:

一個類可能間接繼承了多個接口(例如:public 和internal的接口),這里我們就以實現類為Key,所繼承的接口為value構造一個集合

     /// <summary> /// 獲取程序集中的實現類對應的多個接口/// </summary> /// <param name="assemblyName">程序集</param>public Dictionary<Type, Type[]> GetClassName(string assemblyName){if (!String.IsNullOrEmpty(assemblyName)){Assembly assembly = Assembly.Load(assemblyName);List<Type> ts = assembly.GetTypes().ToList();var result = new Dictionary<Type, Type[]>();foreach (var item in ts.Where(s => !s.IsInterface)){var interfaceType = item.GetInterfaces();result.Add(item, interfaceType);}return result;}return new Dictionary<Type, Type[]>();}

注冊:

我們現在可以把具體的注冊例如

services.AddScoped(typeof(IProductService), typeof(ProductService));

改為:

        //集中注冊服務foreach (var item in GetClassName("Service")){foreach (var typeArray in item.Value){services.AddScoped(typeArray, item.Key);}}

?完整代碼:

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; using Entity.Table; using DAL; using System.Reflection; using Service;namespace ASP.NetCoreAPI {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.AddDbContext<ProductContext>(options =>options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//添加Mysql支持//集中注冊服務foreach (var item in GetClassName("Service")){foreach (var typeArray in item.Value){services.AddScoped(typeArray, item.Key);}}services.AddUnitOfWork<ProductContext>();//添加UnitOfWork支持//services.AddScoped(typeof(IProductService), typeof(ProductService));//用ASP.NET Core自帶依賴注入(DI)注入使用的類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();}/// <summary> /// 獲取程序集中的實現類對應的多個接口/// </summary> /// <param name="assemblyName">程序集</param>public Dictionary<Type, Type[]> GetClassName(string assemblyName){if (!String.IsNullOrEmpty(assemblyName)){Assembly assembly = Assembly.Load(assemblyName);List<Type> ts = assembly.GetTypes().ToList();var result = new Dictionary<Type, Type[]>();foreach (var item in ts.Where(s => !s.IsInterface)){var interfaceType = item.GetInterfaces();result.Add(item, interfaceType);}return result;}return new Dictionary<Type, Type[]>();}} }

轉載于:https://www.cnblogs.com/pxtxws/p/8975173.html

總結

以上是生活随笔為你收集整理的利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。