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

來源:互聯網
上載者:User

引用的命名空間如下:

建立一個協助類:

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{....}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.