IOC的理解,整合AOP,解耦对Service层和Dal层的依赖
?DIP依賴(lài)倒置原則:系統(tǒng)架構(gòu)時(shí),高層模塊不應(yīng)該依賴(lài)于低層模塊,二者通過(guò)抽象來(lái)依賴(lài)
依賴(lài)抽象,而不是細(xì)節(jié)
?貫徹依賴(lài)倒置原則,左邊能抽象,右邊實(shí)例化的時(shí)候不能直接用抽象,所以需要借助一個(gè)第三方
?高層本來(lái)是依賴(lài)低層,但是可以通過(guò)工廠(chǎng)(容器)來(lái)決定細(xì)節(jié),去掉了對(duì)低層的依賴(lài)
?IOC控制反轉(zhuǎn):把高層對(duì)低層的依賴(lài),轉(zhuǎn)移到第三方?jīng)Q定,避免高層對(duì)低層的直接依賴(lài)(是一種目的)
那么程序架構(gòu)就具備良好擴(kuò)展性和穩(wěn)定性
DI依賴(lài)注入:是用來(lái)實(shí)現(xiàn)IOC的一種手段,
?在構(gòu)造對(duì)象時(shí),可以自動(dòng)的去初始化,對(duì)象需要的對(duì)象
構(gòu)造函數(shù)注入 屬性注入 方法注入,IOC容器初始化ApplePhone的時(shí)候 通過(guò)配置文件實(shí)例化 屬性,方法,構(gòu)造函數(shù)
?
不管是構(gòu)造對(duì)象,還是注入對(duì)象,這里都是靠反射做到的
有了依賴(lài)注入,才可能做到無(wú)限層級(jí)的依賴(lài)抽象,才能做到控制反轉(zhuǎn)
?
IOC Unity容器 可以通過(guò)代碼注冊(cè)或配置文件注冊(cè)接口對(duì)應(yīng)實(shí)現(xiàn)類(lèi),實(shí)現(xiàn)了不依賴(lài)具體,可以對(duì)對(duì)象全局單例,線(xiàn)程單例
例子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的類(lèi)庫(kù)(實(shí)現(xiàn)1.0的原有接口)dll拿過(guò)來(lái)即可使用,代碼不做任何修改
例子2 業(yè)務(wù)擴(kuò)展,新加功能
應(yīng)該是加幾個(gè)接口和實(shí)現(xiàn)類(lèi)的映射,就可以解決了。
例子3 實(shí)現(xiàn)AOP
方法需要加日志,加異常管理,可以不修改原有代碼,直接新加異常管理類(lèi)等的類(lèi)庫(kù),在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("無(wú)異常");}else{Console.WriteLine($"異常:{methodReturn.Exception.Message}");}return methodReturn;}public bool WillExecute{get { return true; }}} }?
例子4 數(shù)據(jù)訪(fǎng)問(wèn)層的替換,因?yàn)橐呀?jīng)不依賴(lài)具體實(shí)現(xiàn),把配置文件的接口對(duì)應(yīng)的數(shù)據(jù)訪(fǎng)問(wèn)層實(shí)現(xiàn)類(lèi)替換即可,配置文件格式為InterFace Map 實(shí)現(xiàn)類(lèi)
數(shù)據(jù)訪(fǎng)問(wèn)層的封裝公共增刪改查,Unity 管理 EF DBcontext,保持全局或線(xiàn)程單例還沒(méi)有看到,最近在學(xué)內(nèi)存管理和.Net垃圾回收
?
轉(zhuǎn)載于:https://www.cnblogs.com/kongsq/p/9751716.html
總結(jié)
以上是生活随笔為你收集整理的IOC的理解,整合AOP,解耦对Service层和Dal层的依赖的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 车辆保险怎么看是否到期
- 下一篇: JAVA遇见HTML——JSP篇(JSP