最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET Core實(shí)現(xiàn)自動(dòng)依賴注入

 更新時(shí)間:2021年04月16日 09:18:14   作者:青城同學(xué)  
這篇文章主要介紹了ASP.NET Core實(shí)現(xiàn)自動(dòng)依賴注入的示例,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下

在開(kāi)發(fā).NET Core web服務(wù)的時(shí)候,我們習(xí)慣使用自帶的依賴注入容器來(lái)進(jìn)行注入。

于是就會(huì)經(jīng)常進(jìn)行一個(gè)很頻繁的的重復(fù)動(dòng)作:定義一個(gè)接口->寫(xiě)實(shí)現(xiàn)類->注入

有時(shí)候會(huì)忘了寫(xiě)Add這一步,看到屏幕上的報(bào)錯(cuò)一臉懵逼,然后瞬間反應(yīng)過(guò)來(lái)忘了注入了。趕緊補(bǔ)上serviceCollection.AddXXX這句話

雖然說(shuō)有很多開(kāi)源框架已經(jīng)實(shí)現(xiàn)了類似的工作,比如AutoFac,Unity等依賴注入框架。但是這些庫(kù)都太龐大了,我個(gè)人還是喜歡輕量級(jí)的實(shí)現(xiàn)。

定義一個(gè)枚舉

 
[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
    }
 

掃描運(yùn)行目錄下所有的dll,進(jìn)行自動(dòng)注入

 
/// <summary>
    /// 自動(dòng)依賴注入
    /// </summary>
    public static class AutoInject
    {
        /// <summary>
        /// 自動(dòng)注入所有的程序集有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;
        }
    }
 

使用自動(dòng)依賴注入功能

 
   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();
        }
    }
 

再次運(yùn)行程序,所有的貼有AutoInject的所有的實(shí)現(xiàn)類,都會(huì)被注入到asp.net core的依賴注入容器中。

以上就是ASP.NET Core實(shí)現(xiàn)自動(dòng)依賴注入的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core 自動(dòng)依賴注入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

张家界市| 广安市| 宜丰县| 湘西| 宣威市| 交口县| 昌都县| 松阳县| 河北区| 敦化市| 屏东市| 钦州市| 时尚| 蓬安县| 南阳市| 蓬溪县| 诸城市| 黄梅县| 陆川县| 科技| 土默特右旗| 托里县| 福清市| 武宁县| 鄂托克前旗| 明光市| 汉寿县| 太谷县| 龙门县| 陵川县| 孝义市| 贺兰县| 财经| 崇信县| 五大连池市| 永寿县| 盐池县| 府谷县| 桑日县| 荆门市| 屏南县|