ABP 架構從源碼學習——abp架構啟動核心類AbpBootstrapper(2)

來源:互聯網
上載者:User

標籤:

在AbpBootstrapper中的兩個至關重要的屬性:IIocManager 和 IAbpModuleManager 

 1   public class AbpBootstrapper : IDisposable 2     { 3         /// <summary> 4         /// Gets IIocManager object used by this class. 5         /// </summary> 6         public IIocManager IocManager { get; private set; } 7  8         /// <summary> 9         /// Is this object disposed before?10         /// </summary>11         protected bool IsDisposed;12 13         private IAbpModuleManager _moduleManager;14 //.........15 }
View Code

IIocManager內部封裝了一個Castle的依賴注入容器IWindsorContainer(abp使用的是Castle windor架構,在此架構的基礎上做了一些封裝),

所有類型的註冊,解析還有後面實現的AOP機制的攔截器都是註冊在該容器中的,將具體的註冊還有解析功能分別包含在其父介面IIocRegistrar和IIocResolver中

 1  public interface IIocManager : IIocRegistrar, IIocResolver, IDisposable 2     { 3         /// <summary> 4         /// Reference to the Castle Windsor Container. 5         /// </summary> 6         IWindsorContainer IocContainer { get; } 7  8         /// <summary> 9         /// Checks whether given type is registered before.10         /// </summary>11         /// <param name="type">Type to check</param>12         new bool IsRegistered(Type type);13 14         /// <summary>15         /// Checks whether given type is registered before.16         /// </summary>17         /// <typeparam name="T">Type to check</typeparam>18         new bool IsRegistered<T>();19     }
View Code

其中IIocRegistrar類的類圖如下:

 

1.AddConventionalRegistrar,

2.RegisterAssemblyByConvention(Assembly assembly),

3.RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)

以上 三個方法需要特別注意:

 

第一個方法,AddConventionalRegistrar是向IocManager的一個私人泛型集合List<IConventionalDependencyRegistrar>註冊註冊機制,這句話不太好明白,簡單說就是設定依賴注入模組的注入方式,

通常所有的Module類的預初始化方法中調用以決定哪些類型需要被註冊(如果沒有就無需調用),

比如在Abp程式集中的BasicConventionalRegistrar實現的就是搜尋並註冊指定的程式集中的所有實現了ITransientDependency,ISingletonDependency和IInterceptor的類並註冊到依賴容器中,

 

第二,第三個方法執行真正的註冊邏輯,

通常在一個個具體的Module的初始化方法中調用,傳入當前Module所屬的程式集,

迭代List<IConventionalDependencyRegistrar>將當前程式集作為參數執行註冊,

第二,第三個方法的區別在於第三個方法多了一個ConventionalRegistrationConfig參數,以決定是否還需要搜尋當前程式及中的IWindsorInstaller的實作類別進行註冊,預設是需要的。

 

IAbpModuleManager主要用於管理所有的模組預設也就是一個個的程式集(一個模組對應一個程式集),主要用於搜尋到所有的Module以及他們的依賴Module,首先執行所有Module的PreInitialize方法再執行所有的Initialize,最後執行所有的PostInitialize,執行IAbpModuleManager的ShutdownModules時順序顛倒依次執行所有具體Module的ShutDown方法。

 

ABP 架構從源碼學習——abp架構啟動核心類AbpBootstrapper(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.