轉載來源 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[]>();
}
}
}
