欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core實現(xiàn)自動依賴注入

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

在開發(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)文章

最新評論