遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

Unity2.0与interception的配合使用(EntLib5.0)

引用的命名空间如下:

建立一个帮助类:

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

/// <summary>
    /// 提供对UnityContainer的分装
    /// </summary>
    public sealed class OB
    {
        private static IUnityContainer _Container = null;
        /// <summary>
        /// 需要在Web.config 或 app.config中设置unity
        /// </summary>
        public static IUnityContainer Container
        {
            get
            {
                if (_Container == null)
                {
                    InitContainer();
                }
                return _Container;
            }
        }
        public static T R<T>()
        {
            return Container.Resolve<T>();
        }
        private static void InitContainer()
        {
            if (_Container == null)
            {
                lock (typeof(OB))
                {

                    _Container = new UnityContainer();
                    _Container.LoadConfiguration();
                }
            }
        }
    }

//============================

配置文件部分

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <assembly name="F.Studio.BLL"/> <!-- 可以导入程序集,或命名空间,具体粒度看自己需求-->
  <namespace name="F.Studio.BLL"/>
  <namespace name="F.Studio.BLL.CallHandler"/>
  <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
  <container>
    <extension type="Interception"/>
    <!--类型部分 -->

   <!-- 单构造函数的类型注册--->
    <register type="Order">
      <lifetime type="singleton"/>
      <constructor>
        <param name="P1"/>
        <param name="name" value="aaaa"/>
      </constructor>
      <interceptionBehavior type="PolicyInjectionBehavior"/>
      <interceptor type="TransparentProxyInterceptor"/>
    </register>

    <register type="BizObj">
      <interceptionBehavior type="PolicyInjectionBehavior"/>
      <interceptor type="TransparentProxyInterceptor"/>
    </register>
    <interception>
      <policy name="policy-BizObj-1">
        <matchingRule name="invoke-rule" type="MemberNameMatchingRule">
          <constructor>
            <param name="namesToMatch">
              <array type="string[]">
                <value value="Submit"/>
              </array>
            </param>
          </constructor>
        </matchingRule>
        <callHandler name="invoke-handler1" type="OrderCallHandler">
          <property name="Order" value="1"/>
        </callHandler>
      </policy>
      <policy name="policy-BizObj-2">
        <matchingRule name="invoke-rule" type="MemberNameMatchingRule">
          <constructor>
            <param name="namesToMatch">
              <array type="string[]">
                <value value="Agree"/>
                <value value="Modify"/>
                <value value="Reject"/>
              </array>
            </param>
          </constructor>
        </matchingRule>
        <callHandler name="invoke-handler1" type="OrderCallHandler">
          <property name="Order" value="1"/>
        </callHandler>
      </policy>

     <!-- 一个策略里使用多个匹配规则
      <policy name="policy-F.Studio.BLL">
         --><!--
        <matchingRule name="invoke-rule1" type="NamespaceMatchingRule">
          <constructor>
            <param name="namespaceName" value="F.Studio.BLL" />
          </constructor>
        </matchingRule>
       --><!--
        <matchingRule name="invoke-rule2" type="TypeMatchingRule">
          <constructor>
            <param name="typeName" value="F.Studio.Client" />
          </constructor>
        </matchingRule>
        <matchingRule name="invoke-rule3" type="MemberNameMatchingRule">
          <constructor>
            <param name="namesToMatch">--><!-- 方法列表 --><!--
              <array type="string[]">
                <value value="Get3" />
              </array>
            </param>
          </constructor>
        </matchingRule>
        <callHandler name="invoke-handler1" type="AuthorizationCallHandler">
          <lifetime type="singleton" />
          <property name="Order" value="1"/>
        </callHandler>
      </policy>
      -->
    </interception>
  </container>
</unity>

//=============================拦截处理类=========

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity.Utility;

    class AuthorizationCallHandler:ICallHandler, IDisposable
    {
        public AuthorizationCallHandler()
        {

        }

        /// <summary>
        /// 函数调用拦截处理
        /// </summary>
        /// <param name="input"></param>
        /// <param name="getNext"></param>
        /// <returns></returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {

        

            //调用前操作


            IMethodReturn methodReturn = getNext().Invoke(input, getNext);
     
            //调用后操作
            if (methodReturn.Exception == null)
            {
                
                    //"Successfully finished {0}"
            }
            else
            {
              
                    //"Finished {0} with exception {1}: {2}",

            }

            return methodReturn;
        }

        private int order;
        public int Order
        {
            get
            {
                return order;
            }
            set
            {
                order = value;
            }
        }

        public void Dispose()
        {
            //释放资源操作
        }
    }

    /// <summary>
    /// 使用特属声明时,需要定义Attribute类
    /// </summary>
    class AuthorizationCallHandlerAttribute : HandlerAttribute
    {
        /// <summary>
        /// 可以给特性类构造函数传递参数
        /// </summary>
        public AuthorizationCallHandlerAttribute()
        {

        }
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new AuthorizationCallHandler();
        }
    }

//===========采用透明代理拦截器=============

public class BizObj:MarshalByRefObject{....}

posted on 2011-02-25 09:46  遗忘海岸  阅读(746)  评论(0编辑  收藏  举报