设为首页 加入收藏

TOP

面向切面编程AOP(二)
2019-09-20 11:46:08 】 浏览:135
Tags:面向 编程 AOP
.Configuration"
/> <containers> <container name="aopContainer"> <extension type="Interception"/> <register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP"> <interceptor type="InterfaceInterceptor"/> <interceptionBehavior type="MyAOP.UnityWay.MonitorBehavior, MyAOP"/> <interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/> <interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/> <interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/> <interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/> <interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/> </register> </container> </containers> </unity> </configuration> View Code

 

 使用EntLib\PIAB Unity 实现动态代理:

 public class UnityConfigAOP
 {
     [Obsolete]
     public static void Show()
     {
         User user = new User()
         {
             Name = "bingle",
             Password = "1234567890123456789"
         };
         //配置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);
         configSection.Configure(container, "aopContainer");

         IUserProcessor processor = container.Resolve<IUserProcessor>();
         processor.RegUser(user);
         processor.GetUser(user);
     }
 }
View Code
public class LogAfterBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Console.WriteLine("LogAfterBehavior");
        foreach (var item in input.Inputs)
        {
            Console.WriteLine(item.ToString());//反射获取更多信息
        }
        IMethodReturn methodReturn = getNext()(input, getNext);
        Console.WriteLine("LogAfterBehavior" + methodReturn.ReturnValue);
        return methodReturn;
    }

    public bool WillExecute
    {
        get { return true; }
    }
}
View Code
 /// <summary>
 /// 不需要特性
 /// </summary>
 public class LogBeforeBehavior : IInterceptionBehavior
 {
     public IEnumerable<Type> GetRequiredInterfaces()
     {
         return Type.EmptyTypes;
     }

     public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
     {
         Console.WriteLine("LogBeforeBehavior");
         foreach (var item in input.Inputs)
         {
             Console.WriteLine(item.ToString());//反射获取更多信息
         }
         return getNext().Invoke(input, getNext);
     }

     public bool WillExecute
     {
         get { return true; }
     }
 }
View Code
 public clas
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇设计模式-观察者设计模式 下一篇观察者模式,从公众号群发说起

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目