中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity容器使用筆記

發布時間:2020-08-21 23:21:47 來源:網絡 閱讀:864 作者:676386173 欄目:編程語言

1、配置文件Unity容器使用筆記
說明:此處有兩個容器的節點,用來分別初始化兩個容器,可以應對需要注入兩個dbContext的情況。
代碼:
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="yydyoaSection">
<extension type="Interception"/>
<register type="Study.Unity.Interface.IDoWork,Study.Unity" mapTo=" Study.Unity.Service.StudentDoWork,Study.Unity">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="Study.Unity.Aop.ParameterCheckBehavior,Study.Unity"/>
<interceptionBehavior type="Study.Unity.Aop.CachingBehavior,Study.Unity"/>
<interceptionBehavior type="Study.Unity.Aop.ExpessionBehavior,Study.Unity"/>
<interceptionBehavior type="Study.Unity.Aop.LogBeforeBehavior,Study.Unity"/>
</register>
</container>
<container name="managerSection">
<extension type="Interception"/>
<register type="Study.Unity.Interface.IDoWork,Study.Unity" mapTo=" Study.Unity.Service.TeacherDoWork,Study.Unity">
<interceptor type="InterfaceInterceptor"/>
</register>
</container>
</containers>
</unity>
</configuration>

2、初始化容器
說明:創建了一個枚舉,用來對應配置文件中的兩個節點,然后通過擴展方法獲取到枚舉值在配置文件中的節點名稱,用來分別初始化不同的容器。
代碼:
容器的工廠:

  public class DIFactory
    {
        private static readonly object _SyncHelper = new object();
        private static volatile Dictionary<EnContainer, IUnityContainer> _UnityContainerDictionary = new Dictionary<EnContainer, IUnityContainer>();

        public static IUnityContainer GetContainer(EnContainer enContainer)
        {
            if (!_UnityContainerDictionary.ContainsKey(enContainer))
            {
                lock (_SyncHelper)
                {
                    if (!_UnityContainerDictionary.ContainsKey(enContainer))
                    {
                        //配置UnityContainer
                        IUnityContainer container = new UnityContainer();
                        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                        fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
                        Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                        UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                        string strSection = enContainer.Speccn();
                        configSection.Configure(container, strSection);

                        _UnityContainerDictionary.Add(enContainer, container);
                    }
                }
            }
            return _UnityContainerDictionary[enContainer];
        }
    }
    枚舉:
 public enum EnContainer
    {
        [Speccn("yydyoaSection")]
        YYDYOA = 1,
        [Speccn("managerSection")]
        MANAGER = 2
    }
    特性:
     [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
    public class SpeccnAttribute : Attribute
    {
        private string Speccn { get; set; }
        public SpeccnAttribute(string speccn)
        {
            this.Speccn = speccn;
        }
        public string GetSpeccn()
        {
            return this.Speccn;
        }
    }
    擴展方法:   
 public static class EnumExtend
    {
        public static string Speccn(this Enum enContainer)
        {
            Type type = enContainer.GetType();
            FieldInfo field = type.GetField(enContainer.ToString());
            if (field.IsDefined(typeof(SpeccnAttribute), true))
            {
                SpeccnAttribute speccnAttribute = (SpeccnAttribute)field.GetCustomAttribute(typeof(SpeccnAttribute));
                return speccnAttribute.GetSpeccn();
            }
            else
            {
                return enContainer.ToString();
            }
        }
    }

3、接口和實現類的代碼
接口:

  public interface IDoWork
    {
        string Show(string arg);
    }
    實現類:
 public class StudentDoWork : IDoWork
    {
        public string Show(string arg)
        {
            Console.WriteLine($"{this.GetType().Name}_DoWork Before");
            Console.WriteLine($"{this.GetType().Name}_DoWork After");
            return nameof(StudentDoWork);
        }
    }

        public class TeacherDoWork : IDoWork
    {
        public string Show(string arg)
        {
            Console.WriteLine($"{this.GetType().Name}_DoWork");
            return nameof(TeacherDoWork);
        }
    }
    4、AOP擴展類的代碼:
        參數檢查:
          public class ParameterCheckBehavior : IInterceptionBehavior
    {
        public bool WillExecute => true;

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("ParameterCheckBehavior");
            if (input.Inputs[0].ToString().Length < 10)//可以過濾一下敏感詞
            {
                //返回一個異常
                return input.CreateExceptionMethodReturn(new Exception("密碼長度不能小于10位"));
            }
            else
            {
                Console.WriteLine("參數檢測無誤");
                return getNext().Invoke(input, getNext);
            }
        }
    }
    日志:
 public class LogBeforeBehavior : IInterceptionBehavior
    {
        public bool WillExecute => true;

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("LogBehavior before");
            IMethodReturn method = getNext()(input, getNext);
            Console.WriteLine("LogBehavior after");
            return method;
        }
    }
    異常:
 public class ExpessionBehavior : IInterceptionBehavior
    {
        public bool WillExecute => true;

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("ExpessionBehavior before");

            IMethodReturn method = getNext()(input, getNext);
            if (method.Exception != null)
                Console.WriteLine($"異常:{method.Exception.Message}");
            Console.WriteLine("ExpessionBehavior after");
            return method;
        }
    }
    緩存:
  public class CachingBehavior : IInterceptionBehavior
    {
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        private static Dictionary<string, object> CachingBehaviorDictionary = new Dictionary<string, object>();

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            string key = $"{input.MethodBase.Name}_{Newtonsoft.Json.JsonConvert.SerializeObject(input.Inputs)}";
            if (CachingBehaviorDictionary.ContainsKey(key))
            {
                return input.CreateMethodReturn(CachingBehaviorDictionary[key]);
            }
            else
            {
                IMethodReturn result = getNext().Invoke(input, getNext);
                if (result.ReturnValue != null)
                {
                    CachingBehaviorDictionary.Add(key, result.ReturnValue);
                    Console.WriteLine("CachingBehavior");
                }
                return result;
            }
        }

        public bool WillExecute
        {
            get { return true; }
        }
    }
    5、測試代碼
 static void Main(string[] args)
        {
            IUnityContainer yydyoaContainer = DIFactory.GetContainer(EnContainer.YYDYOA);
            IDoWork doWork = yydyoaContainer.Resolve<IDoWork>();
            doWork.Show("12345678901234");
            yydyoaContainer = DIFactory.GetContainer(EnContainer.MANAGER);
            doWork = yydyoaContainer.Resolve<IDoWork>();
            doWork.Show("123");
            Console.ReadKey();
        }
    6、總結
        AOP擴展的進入順序是根據配置文件從上到下進入,業務邏輯與拓展邏輯的執行順序是根據getNext().Invoke(input, getNext)代碼的位置決定。
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

陵川县| 靖州| 梅河口市| 肥乡县| 瓮安县| 靖安县| 维西| 芦溪县| 靖边县| 安图县| 黑水县| 山阴县| 鄢陵县| 梁河县| 门源| 利津县| 怀安县| 丽水市| 延寿县| 西峡县| 安宁市| 普陀区| 阿鲁科尔沁旗| 始兴县| 双桥区| 湘潭县| 青岛市| 诏安县| 桐庐县| 阿勒泰市| 大竹县| 新化县| 深州市| 施甸县| 灌云县| 礼泉县| 淮北市| 天峨县| 黄骅市| 许昌县| 健康|