IOC的理解,整合AOP,解耦对Service层和Dal层的依赖
?DIP依賴倒置原則:系統(tǒng)架構(gòu)時(shí),高層模塊不應(yīng)該依賴于低層模塊,二者通過抽象來依賴
依賴抽象,而不是細(xì)節(jié)
?貫徹依賴倒置原則,左邊能抽象,右邊實(shí)例化的時(shí)候不能直接用抽象,所以需要借助一個(gè)第三方
?高層本來是依賴低層,但是可以通過工廠(容器)來決定細(xì)節(jié),去掉了對(duì)低層的依賴
?IOC控制反轉(zhuǎn):把高層對(duì)低層的依賴,轉(zhuǎn)移到第三方?jīng)Q定,避免高層對(duì)低層的直接依賴(是一種目的)
那么程序架構(gòu)就具備良好擴(kuò)展性和穩(wěn)定性
DI依賴注入:是用來實(shí)現(xiàn)IOC的一種手段,
?在構(gòu)造對(duì)象時(shí),可以自動(dòng)的去初始化,對(duì)象需要的對(duì)象
構(gòu)造函數(shù)注入 屬性注入 方法注入,IOC容器初始化ApplePhone的時(shí)候 通過配置文件實(shí)例化 屬性,方法,構(gòu)造函數(shù)
?
不管是構(gòu)造對(duì)象,還是注入對(duì)象,這里都是靠反射做到的
有了依賴注入,才可能做到無限層級(jí)的依賴抽象,才能做到控制反轉(zhuǎn)
?
IOC Unity容器 可以通過代碼注冊(cè)或配置文件注冊(cè)接口對(duì)應(yīng)實(shí)現(xiàn)類,實(shí)現(xiàn)了不依賴具體,可以對(duì)對(duì)象全局單例,線程單例
例子1
Service業(yè)務(wù)邏輯層升級(jí),在原有1.0的基礎(chǔ)上添加一些功能,使用配置文件注冊(cè)
<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的類庫(實(shí)現(xiàn)1.0的原有接口)dll拿過來即可使用,代碼不做任何修改
例子2 業(yè)務(wù)擴(kuò)展,新加功能
應(yīng)該是加幾個(gè)接口和實(shí)現(xiàn)類的映射,就可以解決了。
例子3 實(shí)現(xiàn)AOP
方法需要加日志,加異常管理,可以不修改原有代碼,直接新加異常管理類等的類庫,在Unity配置文件添加AOP配置節(jié)點(diǎn)即可實(shí)現(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>?貼一個(gè)異常處理的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ù)訪問層的替換,因?yàn)橐呀?jīng)不依賴具體實(shí)現(xiàn),把配置文件的接口對(duì)應(yīng)的數(shù)據(jù)訪問層實(shí)現(xiàn)類替換即可,配置文件格式為InterFace Map 實(shí)現(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车辆保险怎么看是否到期
- 下一篇: JAVA遇见HTML——JSP篇(JSP