ASP.NET Core實現(xiàn)自動依賴注入
在開發(fā).NET Core web服務(wù)的時候,我們習(xí)慣使用自帶的依賴注入容器來進(jìn)行注入。
于是就會經(jīng)常進(jìn)行一個很頻繁的的重復(fù)動作:定義一個接口->寫實現(xiàn)類->注入
有時候會忘了寫Add這一步,看到屏幕上的報錯一臉懵逼,然后瞬間反應(yīng)過來忘了注入了。趕緊補上serviceCollection.AddXXX這句話
雖然說有很多開源框架已經(jīng)實現(xiàn)了類似的工作,比如AutoFac,Unity等依賴注入框架。但是這些庫都太龐大了,我個人還是喜歡輕量級的實現(xiàn)。
定義一個枚舉
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class AutoInjectAttribute : Attribute { public AutoInjectAttribute(Type interfaceType, InjectType injectType) { Type = interfaceType; InjectType = injectType; } public Type Type { get; set; } /// <summary> /// 注入類型 /// </summary> public InjectType InjectType { get; set; } }
定義三種注入類型
/// <summary> /// 注入類型 /// </summary> public enum InjectType { Scope, Single, Transient }
掃描運行目錄下所有的dll,進(jìn)行自動注入
/// <summary> /// 自動依賴注入 /// </summary> public static class AutoInject { /// <summary> /// 自動注入所有的程序集有InjectAttribute標(biāo)簽 /// </summary> /// <param name="serviceCollection"></param> /// <returns></returns> public static IServiceCollection AddAutoDi(this IServiceCollection serviceCollection) { var path = AppDomain.CurrentDomain.BaseDirectory; var assemblies = Directory.GetFiles(path, "*.dll").Select(Assembly.LoadFrom).ToList(); foreach (var assembly in assemblies) { var types = assembly.GetTypes().Where(a => a.GetCustomAttribute<AutoInjectAttribute>() != null) .ToList(); if (types.Count <= 0) continue; foreach (var type in types) { var attr = type.GetCustomAttribute<AutoInjectAttribute>(); if (attr?.Type == null) continue; switch (attr.InjectType) { case InjectType.Scope: serviceCollection.AddScoped(attr.Type, type); break; case InjectType.Single: serviceCollection.AddSingleton(attr.Type, type); break; case InjectType.Transient: serviceCollection.AddTransient(attr.Type, type); break; default: throw new ArgumentOutOfRangeException(); } } } return serviceCollection; } }
使用自動依賴注入功能
public void ConfigureServices(IServiceCollection services) { services.AddAutoDi(); }
public interface ITest { string Say(); } [AutoInject(typeof(ITest),InjectType.Scope)] public class Test : ITest { public String Say() { return "test:"+DateTime.Now.ToString(); } }
再次運行程序,所有的貼有AutoInject
的所有的實現(xiàn)類,都會被注入到asp.net core的依賴注入容器中。
以上就是ASP.NET Core實現(xiàn)自動依賴注入的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core 自動依賴注入的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ASP.NET的適配器設(shè)計模式(Adapter)應(yīng)用詳解
有關(guān)設(shè)計模式的適配器模式(Adapter)確實不是很好理解理解,接下來將做一個簡單的例子簡要說明下,感興趣的朋友可不要錯過了哈,希望本文可以幫助到你更好的理解適配器設(shè)計模式2013-02-02使用 Salt + Hash 將密碼加密后再存儲進(jìn)數(shù)據(jù)庫
如果你需要保存密碼(比如網(wǎng)站用戶的密碼),你要考慮如何保護(hù)這些密碼數(shù)據(jù),象下面那樣直接將密碼寫入數(shù)據(jù)庫中是極不安全的,因為任何可以打開數(shù)據(jù)庫的人,都將可以直接看到這些密碼2012-12-12ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法
這篇文章主要介紹了ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04ASP.NET Core自定義本地化教程之從文本文件讀取本地化字符串
使用 ASP.NET Core 創(chuàng)建多語言網(wǎng)站,可讓網(wǎng)站擁有更多受眾。下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core自定義本地化教程之從文本文件讀取本地化字符串的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09完美解決Could not load file or assembly AjaxPro.2 or one of its
Could not load file or assembly AjaxPro.2,經(jīng)排查原來是mcafee限制了2007-08-08asp.net訪問Access數(shù)據(jù)庫溢出錯誤
asp.net訪問Access數(shù)據(jù)庫溢出錯誤,大家會的幫忙解決下啊。2009-07-07ASP.Net2.0 GridView 多列排序,顯示排序圖標(biāo),分頁
ASP.Net2.0 GridView 多列排序,顯示排序圖標(biāo),分頁...2006-09-09使用ASP.NET一般處理程序或WebService返回JSON的實現(xiàn)代碼
今天, 將為大家說明如何在 ASP.NET 中使用一般處理程序或者 WebService 向 javascript 返回 JSON2011-10-10