1. 程式人生 > >IOC的理解,整合AOP,解耦對Service層和Dal層的依賴

IOC的理解,整合AOP,解耦對Service層和Dal層的依賴

 DIP依賴倒置原則:系統架構時,高層模組不應該依賴於低層模組,二者通過抽象來依賴依賴抽象,而不是細節 貫徹依賴倒置原則,左邊能抽象,右邊例項化的時候不能直接用抽象,所以需要藉助一個第三方 高層本來是依賴低層,但是可以通過工廠(容器)來決定細節,去掉了對低層的依賴 IOC控制反轉:把高層對低層的依賴,轉移到第三方決定,避免高層對低層的直接依賴(是一種目的)那麼程式架構就具備良好擴充套件性和穩定性DI依賴注入:是用來實現IOC的一種手段, 在構造物件時,可以自動的去初始化,物件需要的物件建構函式注入 屬性注入 方法注入,IOC容器初始化ApplePhone的時候 通過配置檔案例項化 屬性,方法,建構函式

    public class ApplePhone : IPhone
    {
        
        public IHeadphone iHeadphone { get; set; }
        public IPower iPower { get; set; }

        public ApplePhone()
        {
            Console.WriteLine("{0}建構函式", this.GetType().Name);
        }
        public void Call()
        {
            Console.WriteLine(
"{0} Extend打電話", this.GetType().Name); ; } [InjectionConstructor]//建構函式注入:預設找引數最多的建構函式 public ApplePhone(IHeadphone headphone) { this.iHeadphone = headphone; Console.WriteLine("{0} 帶引數建構函式", this.GetType().Name); } [Dependency]//屬性注入
public IMicrophone iMicrophone { get; set; } [InjectionMethod]//方法注入 public void Init1234(IPower power) { this.iPower = power; } }

不管是構造物件,還是注入物件,這裡都是靠反射做到的

有了依賴注入,才可能做到無限層級的依賴抽象,才能做到控制反轉

IOC Unity容器 可以通過程式碼註冊或配置檔案註冊介面對應實現類,實現了不依賴具體,可以對物件全域性單例,執行緒單例

例子1

Service業務邏輯層升級,使用配置檔案註冊

      <container name="testContainer1">
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.ApplePhone, Ruanmou.Service"/>
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service" name="Android"/>
        <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service"/>
        <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service"/>
        <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service"/>
        <register type="Ruanmou.IDAL.IBaseDAL, Ruanmou.IDAL" mapTo="Ruamou.DAL.BaseDAL, Ruamou.DAL"/>
      </container>

      <container name="testContainer">
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend" name="Android"/>
        <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.IDAL.IBaseDAL, Ruanmou.IDAL" mapTo="Ruamou.DAL.BaseDAL, Ruamou.DAL"/>
      </container>

只需要把服務2.0的類庫(實現1.0的原有介面)dll拿過來即可使用,程式碼不做修改

例子2 業務擴充套件,新加功能

應該是加幾個介面和實現類的對映,就可以解決了。

例子3 實現AOP

方法需要加日誌,加異常管理,可以不修改原有程式碼,直接新加異常管理類等的類庫,在Unity配置檔案新增AOP配置節點即可實現

配置檔案配置,

      <container name="testContainerAOP">
        <extension type="Interception"/>
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend">
          <interceptor type="InterfaceInterceptor"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.AuthorizeBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.SmsBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.ExceptionLoggingBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.CachingBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.LogBeforeBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.ParameterCheckBehavior, Ruanmou.Framework"/>
          <interceptionBehavior type="Ruanmou.Framework.AOP.LogAfterBehavior, Ruanmou.Framework"/>
        </register>
        <register type="Ruanmou.Interface.IPhone,Ruanmou.Interface" mapTo="Ruanmou.Service.AndroidPhone, Ruanmou.Service.Extend" name="Android"/>
        <register type="Ruanmou.Interface.IMicrophone, Ruanmou.Interface" mapTo="Ruanmou.Service.Microphone, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.Interface.IHeadphone, Ruanmou.Interface" mapTo="Ruanmou.Service.Headphone, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.Interface.IPower, Ruanmou.Interface" mapTo="Ruanmou.Service.Power, Ruanmou.Service.Extend"/>
        <register type="Ruanmou.IDAL.IBaseDAL, Ruanmou.IDAL" mapTo="Ruamou.DAL.BaseDAL, Ruamou.DAL">
        </register>
      </container>

例子4 資料訪問層的替換,因為已經不依賴具體實現,把配置檔案的介面對應的資料訪問層實現類替換即可,配置檔案格式為InterFace Map 實現類

資料訪問層的封裝公共增刪改查,Unity 管理 EF DBcontext,保持全域性或執行緒單例還沒有看到,最近在學記憶體管理和.Net垃圾回收