91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

高邑县| 察哈| 克拉玛依市| 师宗县| 治县。| 芮城县| 化州市| 新竹市| 阿城市| 秦皇岛市| 陆川县| 尼勒克县| 凉城县| 汕头市| 吴堡县| 正宁县| 文化| 吉林省| 全椒县| 西吉县| 广西| 巩留县| 九龙县| 高邮市| 襄汾县| 南汇区| 清原| 丰城市| 方山县| 岳普湖县| 马龙县| 庄河市| 郧西县| 西吉县| 土默特左旗| 湘西| 阿拉善盟| 康定县| 罗甸县| 大港区| 苏尼特左旗|