Unity使用案例(文件配置)


untiy 從nuget上下載

項目為簡單三層架構。 接口--業務邏輯層--數據訪問層--數據實體層。

接口定義 業務層和數據訪問層 接口。

需求實現使用Unity 能夠集成異常管理

一、介紹一下業務邏輯

  客戶端 調用bll.GetList();獲取后台數據。bll中調用的是dal.GetList();

  我們希望在 調用GetList 系統能夠自動處理異常。不要再在每個方法中都去寫

  try{}catch{}。這樣很麻煩業務邏輯也顯得臃腫。

二、定義異常標簽,異常處理handler

  要想解決以上問題,可以用Unity的攔截機制這樣做,定義異常特性或者叫屬性

namespace Common
{
    public class ExceptionExpandAttribute : HandlerAttribute
    {
        public int ID { get; set; }

        // 摘要:
        //     Derived classes implement this method. When called, it creates a new call
        //     handler as specified in the attribute configuration.
        //
        // 參數:
        //   container:
        //     The Microsoft.Practices.Unity.IUnityContainer to use when creating handlers,
        //     if necessary.
        //
        // 返回結果:
        //     A new call handler object.
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            var handler = container.Resolve<ExceptionHandler>();
            handler.Order = this.Order;
            handler.ID = ID;
            return handler;
        }
    }
}

  建立異常處理handler

public class ExceptionHandler:ICallHandler
    {
        public int Order { get; set; }
        public int ID { get; set; }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            if (ID == 100)
                throw new Exception("AOP異常處理出現異常:Dal出現異常");
            var methodReturn = getNext().Invoke(input, getNext);
            if (methodReturn.Exception != null)
                throw new Exception("AOP異常處理出現異常", methodReturn.Exception);
            
            return methodReturn;
        }
    }

 實現ICallHandler的 Invoke 方法,input 能夠截獲到調用方法傳入的參數,getNext().Invoke(input, getNext); 能夠得到返回值。我們在

getNext().Invoke(input, getNext);此方法執行后可以截獲異常,並進行封裝,這里簡寫。

給要進行異常處理的方法加上標簽

public interface IBll
    {
        [ExceptionExpand(Order=1,ID=10)]
        List<QueryModel> GetList();
    }
public interface IDal
    {
        [ExceptionExpand(Order = 1, ID = 100)]
        List<QueryModel> GetList();
    }

  

然后,前台在配置文件中配置各個對象

三、配置文件配置

  

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    
      <alias alias="IBll" type="InterfaceDefined.IBll,InterfaceDefined" />
      <alias alias="IDal" type="InterfaceDefined.IDal,InterfaceDefined" />
      
      <container>
      <extension type="Interception" />

      <register type="IBll" mapTo="BusinessEntiy.Bll,BusinessEntiy" >
        <constructor>
          <param name="Name" value="nihao" />
        </constructor>

        <interceptor type="TransparentProxyInterceptor" />
        <policyInjection />

      </register>

      <register type="IDal" mapTo="DataAccess.Dal,DataAccess" >
        <interceptor type="TransparentProxyInterceptor" />
        <policyInjection />
      </register>
     </container>
  </unity>

  很簡單指定注冊類,指定類初始化參數,interceptor類型使用TransparentProxyInterceptor

測試:

  

  其他日志,緩存大同小異,寫的簡單,好理解。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM