zoukankan      html  css  js  c++  java
  • Unity The Member Name Matching Rule

    Unity提供了基于成员名称的匹配规则类型MemberNameMatchingRule,允许根据匹配表达式进行注入。看一个简单示例:

     1 public class MyObject
     2 {
     3   public virtual void DoWork()
     4   {
     5 
     6   }
     7 
     8   public virtual void DoWork2()
     9   {
    10 
    11   }
    12 }
    13 
    14 public sealed class Log4NetHandler : ICallHandler
    15 {
    16   #region ICallHandler Members
    17 
    18   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    19   {
    20     Console.WriteLine(input.MethodBase.Name);
    21 
    22     return getNext()(input, getNext);
    23   }
    24 
    25   public Int32 Order { get; set; }
    26 
    27   #endregion
    28 }
    29 
    30 IUnityContainer unityContainer = new UnityContainer();
    31 
    32 unityContainer.LoadConfiguration();
    33 unityContainer.Configure<Interception>()
    34   .AddPolicy(“MemberNameMatchingRule”)
    35   .AddMatchingRule(new MemberNameMatchingRule(“DoWork”, true))
    36   .AddCallHandler<Log4NetHandler>();
    37 unityContainer.RegisterType<MyObject>(
    38   new Interceptor<VirtualMethodInterceptor>(),
    39   new InterceptionBehavior<PolicyInjectionBehavior>()
    40 );
    41 
    42 MyObject myObject = unityContainer.Resolve<MyObject>();
    43 
    44 myObject.DoWork();
    45 myObject.DoWork2();

    上面的示例只有DoWork被调用。MemberNameMatchingRule提供了符号*用来完成剩余部分的匹配,如果只传递*则表示全部。

    使用*:

     1 IUnityContainer unityContainer = new UnityContainer();
     2 
     3 unityContainer.LoadConfiguration();
     4 unityContainer.Configure<Interception>()
     5   .AddPolicy(“MemberNameMatchingRule”)
     6   .AddMatchingRule(new MemberNameMatchingRule(“*”, true))
     7   .AddCallHandler<Log4NetHandler>();
     8 unityContainer.RegisterType<MyObject>(
     9   new Interceptor<VirtualMethodInterceptor>(),
    10   new InterceptionBehavior<PolicyInjectionBehavior>()
    11 );
    12 
    13 MyObject myObject = unityContainer.Resolve<MyObject>();
    14 
    15 myObject.DoWork();
    16 myObject.DoWork2();

    使用后置*:

     1 IUnityContainer unityContainer = new UnityContainer();
     2 
     3 unityContainer.LoadConfiguration();
     4 unityContainer.Configure<Interception>()
     5   .AddPolicy(“MemberNameMatchingRule”)
     6   .AddMatchingRule(new MemberNameMatchingRule(“Do*”, true))
     7   .AddCallHandler<Log4NetHandler>();
     8 unityContainer.RegisterType<MyObject>(
     9   new Interceptor<VirtualMethodInterceptor>(),
    10   new InterceptionBehavior<PolicyInjectionBehavior>()
    11 );
    12 
    13 MyObject myObject = unityContainer.Resolve<MyObject>();
    14 
    15 myObject.DoWork();
    16 myObject.DoWork2();

    使用多个*:

     1 IUnityContainer unityContainer = new UnityContainer();
     2 
     3 unityContainer.LoadConfiguration();
     4 unityContainer.Configure<Interception>()
     5   .AddPolicy(“MemberNameMatchingRule”)
     6   .AddMatchingRule(new MemberNameMatchingRule(“Do*k*”, true))
     7   .AddCallHandler<Log4NetHandler>();
     8 unityContainer.RegisterType<MyObject>(
     9   new Interceptor<VirtualMethodInterceptor>(),
    10   new InterceptionBehavior<PolicyInjectionBehavior>()
    11 );
    12 
    13 MyObject myObject = unityContainer.Resolve<MyObject>();
    14 
    15 myObject.DoWork();
    16 myObject.DoWork2();

    MemberNameMatchingRule提供了符号?用来通配一个字符。看一个简单的示例:

     1 IUnityContainer unityContainer = new UnityContainer();
     2 
     3 unityContainer.LoadConfiguration();
     4 unityContainer.Configure<Interception>()
     5   .AddPolicy(“MemberNameMatchingRule”)
     6   .AddMatchingRule(new MemberNameMatchingRule(“Do???k”, true))
     7   .AddCallHandler<Log4NetHandler>();
     8 unityContainer.RegisterType<MyObject>(
     9   new Interceptor<VirtualMethodInterceptor>(),
    10   new InterceptionBehavior<PolicyInjectionBehavior>()
    11 );
    12 
    13 MyObject myObject = unityContainer.Resolve<MyObject>();
    14 
    15 myObject.DoWork();
    16 myObject.DoWork2();

    MemberNameMatchingRule提供了[]符号表示一个字符的取值范围,比如DoWork[23]允许DoWork2、DoWork3:

     1 public class MyObject
     2 {
     3   public virtual void DoWork()
     4   {
     5 
     6   }
     7 
     8   public virtual void DoWork2()
     9   {
    10 
    11   }
    12 
    13   public virtual void DoWork3()
    14   {
    15 
    16   }
    17 }
    18 
    19 IUnityContainer unityContainer = new UnityContainer();
    20 
    21 unityContainer.LoadConfiguration();
    22 unityContainer.Configure<Interception>()
    23   .AddPolicy(“MemberNameMatchingRule”)
    24   .AddMatchingRule(new MemberNameMatchingRule(“DoWork[23]“, true))
    25   .AddCallHandler<Log4NetHandler>();
    26 unityContainer.RegisterType<MyObject>(
    27   new Interceptor<VirtualMethodInterceptor>(),
    28   new InterceptionBehavior<PolicyInjectionBehavior>()
    29 );
    30 
    31 MyObject myObject = unityContainer.Resolve<MyObject>();
    32 
    33 myObject.DoWork();
    34 myObject.DoWork2();
    35 myObject.DoWork3();

    配置文件定义如下:

    <unity xmlns=”http://schemas.microsoft.com/practices/2010/unity”>
      <sectionExtension type=”Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration” />
    
      <assembly name=”mscorlib, 2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
      <assembly name=”UnityTest6″ />
    
      <namespace name=”UnityTest6″ />
    
      <container>
        <extension type=”Interception” />
    
        <interception>
          <policy name=”MemberNamePolicy”>
            <matchingRule name=”MemberNameMatchingRule” type=”MemberNameMatchingRule”>
              <constructor>
                <param name=”nameToMatch” value=”DoWork*” />
              </constructor>
            </matchingRule>
            <callHandler name=”Log4NetHandler” type=”Log4NetHandler” />
          </policy>
        </interception>
    
        <register type=”MyObject”>
          <interceptor type=”VirtualMethodInterceptor” />
          <interceptionBehavior type=”PolicyInjectionBehavior” />
        </register>
      </container>
    </unity>
  • 相关阅读:
    jquery中$.get()提交和$.post()提交有区别吗?
    数据库连接池的原理。为什么要使用连接池。
    execute,executeQuery,executeUpdate的区别是什么?
    数据库的三级模式与二级映像
    KMP算法(超容易理解的next数组求法)
    软件危机(含通俗理解帮助记忆)
    O(1)复杂度求一个栈的最小值
    操作系统进程状态模型
    判断单链表是否有环,如果有环则找到其环的入口
    两个单链表判断是否相交
  • 原文地址:https://www.cnblogs.com/junchu25/p/2631618.html
Copyright © 2011-2022 走看看