1,使用Unity首先需要在NuGet上面添加关于Unity的相关插件.
1,Unity.Configuration 2,Unity.Abstractions 3,Unity.Container 4,Unity.Interception 5,Unity.Interception.Configuration (特别注意)
1,特别注意在添加unity文件中需要填加unity.json的文件夹文件的配置如下
1 <configuration> 2 <configSections> 3 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/> 4 <!--Microsoft.Practices.Unity.Configuration.UnityConfigurationSection--> 5 </configSections> 6 <unity> 7 <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/> 8 <containers> 9 <container name="aopContainer"> 10 <extension type="Interception"/> 11 <register type="Aop.UnityWay.IUserProcessor,Aop" mapTo="Aop.UnityWay.UserProcessor,Aop"> 12 <interceptor type="InterfaceInterceptor"/> 13 <interceptionBehavior type="Aop.UnityWay.ExceptionLoggingBehavior, Aop"/> 14 <!--<interceptionBehavior type="Aop.UnityWay.MoniterBehavior, Aop"/> 15 <interceptionBehavior type="Aop.UnityWay.LogBeforeBehavior, Aop"/> 16 <interceptionBehavior type="Aop.UnityWay.ParameterCheckBehavior, Aop"/> 17 <interceptionBehavior type="Aop.UnityWay.CachingBehavior, MyAOP"/>--> 18 19 <!--<interceptionBehavior type="Aop.UnityWay.LogAfterBehavior, Aop"/>--> 20 </register> 21 </container> 22 </containers> 23 </unity> 24 </configuration>
但是想要使用unity进行AOP的配置的话我们需要配置一些相应的信息
fileMap.ExeConfigFilename:来配置Json文件的地址文件
1 //配置UnityContainer 2 IUnityContainer container = new UnityContainer(); 3 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); 4 fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config"); 5 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 6 7 UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); 8 configSection.Configure(container, "aopContainer"); 9 10 IUserProcessor processor = container.Resolve<IUserProcessor>(); 11 processor.RegUser(user);
配置完成之后我们再来解读一下json文件中的相关信息
下面我们来看看程序调用的类应当注意的事项
1, 当前的类必须继承接口IInterceptionBehavior 才能是Aop正常的触发
2, getNext()(input, getNext);执行下一个程序,这里是指aop下的下一个程序
1 public class ExceptionLoggingBehavior : IInterceptionBehavior 2 { 3 public IEnumerable<Type> GetRequiredInterfaces() 4 { 5 return Type.EmptyTypes; 6 } 7 8 public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) 9 { 10 Console.WriteLine("ExceptionLoggingBehavior"); 11 IMethodReturn methodReturn = getNext()(input, getNext); 12 if (methodReturn.Exception == null) 13 { 14 Console.WriteLine("无异常"); 15 } 16 else 17 { 18 Console.WriteLine($"异常:{methodReturn.Exception.Message}"); 19 } 20 return methodReturn; 21 } 22 23 public bool WillExecute 24 { 25 get { return true; } 26 }
2或者使用日常的程序 数据校验将数据的内容和值进行关闭
1 public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) 2 { 3 Console.WriteLine("ParameterCheckBehavior"); 4 //通过特性校验 5 User user = input.Inputs[0] as User; 6 if (user.Password.Length < 10) 7 { 8 //throw new Exception(); 9 return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于10位")); 10 } 11 else 12 { 13 Console.WriteLine("参数检测无误"); 14 return getNext().Invoke(input, getNext); 15 } 16 }