日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOC的理解,整合AOP,解耦对Service层和Dal层的依赖

發(fā)布時間:2025/5/22 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOC的理解,整合AOP,解耦对Service层和Dal层的依赖 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


?DIP依賴倒置原則:系統(tǒng)架構(gòu)時,高層模塊不應(yīng)該依賴于低層模塊,二者通過抽象來依賴
依賴抽象,而不是細(xì)節(jié)

?貫徹依賴倒置原則,左邊能抽象,右邊實例化的時候不能直接用抽象,所以需要借助一個第三方

?高層本來是依賴低層,但是可以通過工廠(容器)來決定細(xì)節(jié),去掉了對低層的依賴
?IOC控制反轉(zhuǎn):把高層對低層的依賴,轉(zhuǎn)移到第三方?jīng)Q定,避免高層對低層的直接依賴(是一種目的)
那么程序架構(gòu)就具備良好擴(kuò)展性和穩(wěn)定性

DI依賴注入:是用來實現(xiàn)IOC的一種手段,
?在構(gòu)造對象時,可以自動的去初始化,對象需要的對象
構(gòu)造函數(shù)注入 屬性注入 方法注入,IOC容器初始化ApplePhone的時候 通過配置文件實例化 屬性,方法,構(gòu)造函數(shù)

using Microsoft.Practices.Unity; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Ruanmou.Interface; using System; using Unity.Attributes;namespace Ruanmou.Service {public class ApplePhone : IPhone{[Dependency]//屬性注入:不錯,但是有對容器的依賴public IMicrophone iMicrophone { get; set; }public IHeadphone iHeadphone { get; set; }public IPower iPower { get; set; }//[InjectionConstructor]public ApplePhone(){Console.WriteLine("{0}構(gòu)造函數(shù)", this.GetType().Name);}//[InjectionConstructor]//構(gòu)造函數(shù)注入:最好的,默認(rèn)找參數(shù)最多的構(gòu)造函數(shù)public ApplePhone(IHeadphone headphone){this.iHeadphone = headphone;Console.WriteLine("{0}帶參數(shù)構(gòu)造函數(shù)", this.GetType().Name);}public void Call(){Console.WriteLine("{0}打電話", this.GetType().Name); }[InjectionMethod]//方法注入:最不好的,增加一個沒有意義的方法,破壞封裝public void Init1234(IPower power){this.iPower = power;}} }

?

不管是構(gòu)造對象,還是注入對象,這里都是靠反射做到的

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

?

IOC Unity容器 可以通過代碼注冊或配置文件注冊接口對應(yīng)實現(xiàn)類,實現(xiàn)了不依賴具體,可以對對象全局單例,線程單例

例子1

Service業(yè)務(wù)邏輯層升級,在原有1.0的基礎(chǔ)上添加一些功能,使用配置文件注冊

<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>

只需要把服務(wù)2.0的類庫(實現(xiàn)1.0的原有接口)dll拿過來即可使用,代碼不做任何修改

例子2 業(yè)務(wù)擴(kuò)展,新加功能

應(yīng)該是加幾個接口和實現(xiàn)類的映射,就可以解決了。

例子3 實現(xiàn)AOP

方法需要加日志,加異常管理,可以不修改原有代碼,直接新加異常管理類等的類庫,在Unity配置文件添加AOP配置節(jié)點即可實現(xiàn)

配置文件配置,

<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>

?貼一個異常處理的AOP例子代碼

namespace Ruanmou.Framework.AOP {public class ExceptionLoggingBehavior : IInterceptionBehavior{public IEnumerable<Type> GetRequiredInterfaces(){return Type.EmptyTypes;}public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext){IMethodReturn methodReturn = getNext()(input, getNext);Console.WriteLine("ExceptionLoggingBehavior");if (methodReturn.Exception == null){Console.WriteLine("無異常");}else{Console.WriteLine($"異常:{methodReturn.Exception.Message}");}return methodReturn;}public bool WillExecute{get { return true; }}} }

?

例子4 數(shù)據(jù)訪問層的替換,因為已經(jīng)不依賴具體實現(xiàn),把配置文件的接口對應(yīng)的數(shù)據(jù)訪問層實現(xiàn)類替換即可,配置文件格式為InterFace Map 實現(xiàn)類

數(shù)據(jù)訪問層的封裝公共增刪改查,Unity 管理 EF DBcontext,保持全局或線程單例還沒有看到,最近在學(xué)內(nèi)存管理和.Net垃圾回收

?

轉(zhuǎn)載于:https://www.cnblogs.com/kongsq/p/9751716.html

總結(jié)

以上是生活随笔為你收集整理的IOC的理解,整合AOP,解耦对Service层和Dal层的依赖的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。