MVC中使用Unity AOP

Unity AOP支持,透明代码,接口方式,与Virtual 方法, 由于透明代理必须继承MarshalByRefObject,接口方式使用Unity构建时,只能按接口获取

Container.Resolve(IXXX),  在让标记接口继承IController(Filter正常)后发现Unity无法拦截到,最后只有Virtal方法方式可以形, Virtual方式的拦截器会在Filter执行后执行,Virtaul方法编织进AOP代码后取代了原来的Controll方法

 

基于虚方法方式,Unity生成的代理类,该方式属于类型拦截,不存在拦截(代理)实例到目标实例(target)的引用

public class Wrapped_Home_273249ec14ea46568e66523bd7e6877c : Home, IInterceptingProxy
{
    // Fields
    private readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();

    // Methods
    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = base.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }

    public override int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this, methodof(Home.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }

    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

 

 

基于接口方式,Unity生成的代理类--注意接口方式(InterfaceInterceptor)跟透明代理(TransparentProxyInterceptor)一样都是实例拦截器

public class Wrapped_IHome_ff5a91d8f8b1447c924a45e62dcb89e8 : IInterceptingProxy, IHome
{
    // Fields
    private readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();
    private IHome target;
    private Type typeToProxy;

    // Methods
    public Wrapped_IHome_ff5a91d8f8b1447c924a45e62dcb89e8(IHome target, Type typeToProxy)
    {
        this.target = target;
        this.typeToProxy = typeToProxy;
    }

    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = this.target.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }

    public int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IHome.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }

    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

 

基于接口的动态生成类

 

  public interface IAOP:IController
    {
        ActionResult Index();
    }

 下面代码中pipeline中只有一个元素那就是PolicyInjectionBehavior,而PolicyInjectionBehavior里面又根据调用的方法来维护一组ICallHandler管道

public class Wrapped_IHome_fa19b49db3a345b28531be422717e4e7 : IInterceptingProxy, IHome, IController
{
    // Fields
    public readonly InterceptionBehaviorPipeline pipeline = new InterceptionBehaviorPipeline();
    private IHome target;
    private Type typeToProxy;

    // Methods
    public Wrapped_IHome_fa19b49db3a345b28531be422717e4e7(IHome target, Type typeToProxy)
    {
        this.target = target;
        this.typeToProxy = typeToProxy;
    }

    [CompilerGenerated]
    private IMethodReturn <Execute_DelegateImplementation>__1(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            this.target.Execute();
            return inputs.CreateMethodReturn(null, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }

    [CompilerGenerated]
    private IMethodReturn <Index_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
    {
        try
        {
            int returnValue = this.target.Index();
            return inputs.CreateMethodReturn(returnValue, new object[0]);
        }
        catch (Exception exception)
        {
            return inputs.CreateExceptionMethodReturn(exception);
        }
    }

    public void Execute()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IController.Execute), new object[0]);
        Exception exception = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Execute_DelegateImplementation>__1)).Exception;
        if (exception != null) throw exception;
    }

    public int Index()
    {
        VirtualMethodInvocation input = new VirtualMethodInvocation(this.target, methodof(IHome.Index), new object[0]);
        IMethodReturn return2 = this.pipeline.Invoke(input, new InvokeInterceptionBehaviorDelegate(this.<Index_DelegateImplementation>__0));
        Exception exception = return2.Exception;
        if (exception != null) throw exception;
        return (int) return2.ReturnValue;
    }

    void IInterceptingProxy.AddInterceptionBehavior(IInterceptionBehavior interceptor)
    {
        this.pipeline.Add(interceptor);
    }
}

 

你可能感兴趣的:(unity)