zoukankan      html  css  js  c++  java
  • Unity Creating Interception Handler Attributes

    Unity提供了一种默认注入CallHandler的机制,只需要开发人员提供继承于HandlerAttribute抽象类,并实现CreateHandler函数,它返回一个实现ICallHandler的类型。你可以通过上下文传递的IUnityContainer创建对应的ICallHandler实现。看一个简单的示例:

     1 public sealed class ConsoleOutHandlerAttribute : HandlerAttribute
     2 {
     3   public override ICallHandler CreateHandler(IUnityContainer container)
     4   {
     5     return container.Resolve<ConsoleOutHandler>();
     6   }
     7 }
     8 
     9 public sealed class ConsoleOutHandler : ICallHandler
    10 {
    11   #region ICallHandler Members
    12 
    13   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    14   {
    15     Console.WriteLine(input.MethodBase.Name);
    16 
    17     return getNext()(input, getNext);
    18   }
    19 
    20   public Int32 Order { get; set; }
    21 
    22   #endregion
    23 }
    24 
    25 public class MyObject
    26 {
    27   [ConsoleOutHandler]
    28   public virtual void DoWork()
    29   {
    30 
    31   }
    32 }
    33 
    34 IUnityContainer unityContainer = new UnityContainer();
    35 
    36 unityContainer.LoadConfiguration();
    37 unityContainer.RegisterType<ICallHandler, ConsoleOutHandler>();
    38 unityContainer.RegisterType<MyObject>(
    39   new Interceptor<VirtualMethodInterceptor>(),
    40   new InterceptionBehavior<PolicyInjectionBehavior>()
    41 );
    42 
    43 MyObject myObject = unityContainer.Resolve<MyObject>();
    44 
    45 myObject.DoWork();
  • 相关阅读:
    第04组 Alpha冲刺 总结
    第04组 Alpha冲刺 (6/6)
    软工实践个人总结
    第09组 每周小结 (3/3)
    第09组 每周小结 (2/3)
    第09组 每周小结 (1/3)
    第09组 Beta冲刺 总结
    第09组 Beta冲刺 (5/5)
    第09组 Beta冲刺 (4/5)
    第09组 Beta冲刺 (3/5)
  • 原文地址:https://www.cnblogs.com/junchu25/p/2633434.html
Copyright © 2011-2022 走看看