廢話少說,先上代碼 public static Dictionary<Type, Type[]> GetImpleAndInterfaces(string assemblyName,string suffix) { if (!String.IsNullOrEmpty(assemblyName)) { ...
廢話少說,先上代碼
public static Dictionary<Type, Type[]> GetImpleAndInterfaces(string assemblyName,string suffix) { if (!String.IsNullOrEmpty(assemblyName)) { Assembly assembly = Assembly.Load(assemblyName); List<Type> types = assembly .GetTypes() .Where(x => !x.IsInterface && x.Name.Contains(suffix) && !x.IsGenericType) .ToList(); var result = new Dictionary<Type, Type[]>(); foreach (var item in types) { var interfaceType = item.GetInterfaces(); result.Add(item, interfaceType); } return result; } return new Dictionary<Type, Type[]>(); }
實際就是載入指定的DLL(根據DLL名字載入),然後過濾出非泛型且符合尾碼的實現類(一般倉儲,業務都有固定的尾碼,可以自己定義命名規則,例如XXXReository, XXXService等),並且找到實現類所繼承的介面(根據我設想,最好是一個介面一個實現,如果遇到一個介面多個實現的地方,儘量自己手工註入,以免引起不必要的誤會),進行單元測試,示例如下
[TestMethod] public void BatchDITestMethod() { var accessImpleAndInterfaces= AssemblyHelper.GetImpleAndInterfaces("DJMS.DataAccess","Access"); foreach(var v in accessImpleAndInterfaces) { Console.WriteLine($"class={v.Key},interface={v.Value[0]},{v.Value.Length}"); } Console.WriteLine($"Access介面及實現個數為:{accessImpleAndInterfaces.Count}"); var logicImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.BusinessLogic", "Logic"); foreach (var v in logicImpleAndInterfaces) { Console.WriteLine($"class={v.Key},interface={v.Value[0]},{v.Value.Length}"); } Console.WriteLine($"Logic介面及實現個數為:{accessImpleAndInterfaces.Count}"); }
單元測試結果,發現沒有問題,都可以找準
class=DJMS.DataAccess.Repository.DepartmentAccess,interface=DJMS.DataAccess.Repository.IDepartmentAccess,1 class=DJMS.DataAccess.Repository.ElemeTokenAccess,interface=DJMS.DataAccess.Repository.IElemeTokenAccess,1 class=DJMS.DataAccess.Repository.MenuAccess,interface=DJMS.DataAccess.Repository.IMenuAccess,1 class=DJMS.DataAccess.Repository.NewsAccess,interface=DJMS.DataAccess.Repository.INewsAccess,1 class=DJMS.DataAccess.Repository.NewsTypeAccess,interface=DJMS.DataAccess.Repository.INewsTypeAccess,1 class=DJMS.DataAccess.Repository.RoleAccess,interface=DJMS.DataAccess.Repository.IRoleAccess,1 class=DJMS.DataAccess.Repository.RoleMenuAccess,interface=DJMS.DataAccess.Repository.IRoleMenuAccess,1 class=DJMS.DataAccess.Repository.SMSAccess,interface=DJMS.DataAccess.Repository.ISMSAccess,1 class=DJMS.DataAccess.Repository.UserAccess,interface=DJMS.DataAccess.Repository.IUserAccess,1 class=DJMS.DataAccess.Repository.UserRoleAccess,interface=DJMS.DataAccess.Repository.IUserRoleAccess,1 Access介面及實現個數為:10 class=DJMS.BusinessLogic.User.UserLogic,interface=DJMS.BusinessLogic.User.IUserLogic,1 class=DJMS.BusinessLogic.UserRole.UserRoleLogic,interface=DJMS.BusinessLogic.UserRole.IUserRoleLogic,1 class=DJMS.BusinessLogic.SMS.SMSLogic,interface=DJMS.BusinessLogic.SMS.ISMSLogic,1 class=DJMS.BusinessLogic.Role.RoleLogic,interface=DJMS.BusinessLogic.Role.IRoleLogic,1 class=DJMS.BusinessLogic.Photo.PhotoLogic,interface=DJMS.BusinessLogic.Photo.IPhotoLogic,1 class=DJMS.BusinessLogic.News.NewsLogic,interface=DJMS.BusinessLogic.News.INewsLogic,1 class=DJMS.BusinessLogic.NewsType.NewsTypeLogic,interface=DJMS.BusinessLogic.NewsType.INewsTypeLogic,1 class=DJMS.BusinessLogic.Menu.MenuLogic,interface=DJMS.BusinessLogic.Menu.IMenuLogic,1 class=DJMS.BusinessLogic.Eleme.ElemeLogic,interface=DJMS.BusinessLogic.Eleme.IElemeLogic,1 class=DJMS.BusinessLogic.Department.DepartmentLogic,interface=DJMS.BusinessLogic.Department.IDepartmentLogic,1 class=DJMS.BusinessLogic.Base.BaseLogic,interface=DJMS.BusinessLogic.Base.IBaseLogic,1 Logic介面及實現個數為:10
然後批量註入並測試,測試一切正常
//批量介面註入
var accessImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.DataAccess", "Access");
foreach (var v in accessImpleAndInterfaces)
services.AddScoped(v.Value[0], v.Key);
var logicImpleAndInterfaces = AssemblyHelper.GetImpleAndInterfaces("DJMS.BusinessLogic", "Logic");
foreach (var v in logicImpleAndInterfaces)