ABP之模組分析

2021-09-24 17:25:43 字數 2960 閱讀 8520

本篇作為我abp介紹的第三篇文章,這次想講下模組的,abp文件已經有模組這方面的介紹,但是它只講到如何使用模組,我想詳細講解下它模組的設計思路。

abp 框架提供了建立和組裝模組的基礎,乙個模組能夠依賴於另乙個模組。在通常情況 下,乙個程式集就可以看成是乙個模組。在 abp 框架中,乙個模組通過乙個類來定義,而這 個類要繼承自 abpmodule。

其實它的設計思路很簡單:

1、載入bin目錄下的所有dll

public class webassemblyfinder : iassemblyfinder

}return assembliesinbinfolder;}}

2、迴圈判斷獲取所有與abpmodule的types有關

public static bool isabpmodule(type type)

並遞迴獲取沒有只在所有的dependsonattribute,把他們填在到modules集合中(請詳細看abpmodule.finddependedmoduletypes方法)

private static icollectionaddmissingdependedmodules(icollectionallmodules)

return allmodules;

}private static void filldependedmodules(type module, icollectionallmodules)}}

public static listfinddependedmoduletypes(type moduletype)

var list = new list();

if (moduletype.isdefined(typeof(dependsonattribute), true))}}

return list;

}

所有關於模組的重要**都在abpmodulemanager中,在上面我們已經載入了所有的模組的型別,那麼abp到底有多少個modules呢

3、既然我得到了所有的moduletypes了,那麼我就通過castle windsor迴圈註冊了,並反轉

private void loadall()

if (!_iocmanager.isregistered(moduletype))

}//模組反轉並新增到_modules中 add to module collection

foreach (var moduletype in moduletypes)

//確保abpkernelmodule是_modules中第乙個module,abpkernelmodule must be the first module

var startupmoduleindex = _modules.findindex(m => m.type == typeof(abpkernelmodule));

if (startupmoduleindex > 0)

setdependencies();

logger.debugformat(" modules loaded.", _modules.count);

}

上面**有注釋,詳細請看注釋

4、就是初始化所有模組的事件,早abpmodule中作者定義了三個事件,在實踐應用在我們會依次執行下面三個方法

public virtual void preinitialize()

/// /// this method is used to register dependencies for this module.

///

public virtual void initialize()

///

public virtual void postinitialize()

在乙個應用中,abp 框架呼叫了 module 模組的一些指定的方法來進行啟動和關閉模組的 操作。我們可以過載這些方法來完成我們自己的任務。 abp 框架通過依賴關係的順序來呼叫這些方法,

假如:模組 a 依賴於模組 b,那麼模組 b 要在模組 a 之前初始化,模組啟動的方法順序如下:

1) preinitialize-b

2) preinitialize-a

3) initialize-b

4) initialize-a

5) postinitialize-b

6) postinitialize-a

那麼我們是怎麼執行上面的方法的呢,方案在abpmodulemanager的initializemodules方法中

public virtual void initializemodules()

那麼我們在自定義模組的時候是要重寫上面三個方法的,有點像管道的事件,我們會依次執行這些事件,現在我們隨便看個abpentityframeworkmodule模組它重寫了其中兩個

public override void preinitialize()

public override void initialize()

一般的我們都要在initialize方法中加上這句iocmanager.registerassemblybyconvention(assembly.getexecutingassembly()); 也就是註冊當前程式集,ioc初始化用的哦,好了,至此終於把abp模組的思路講完啦,大家可以配合abp的文件的相關章節進行研究,希望對初學者有幫助。

ABP理論學習之模組系統

返回總目錄 abp提供了構建模組並將這些模組組合起來建立應用的基礎設施。乙個模組可以依賴另乙個模組。一般來說,乙個程式集可以認為是乙個模組。乙個模組是由乙個派生了abpmodule的類定義的。比如說我們在開發乙個可以用在不同的應用中的部落格模組。最簡單的模組定義如下 abp掃瞄所有的程式集,並找出所...

ABP啟動流程分析

public iserviceprovider configureservices iservicecollection services 先來看看startup這個類的configureservices方法,注意這個方法的返回值,我們知道使用vs建立的專案 返回值為void,但是使用abp的話 返...

ABP官方文件 三 模組系統

abp框架提供了建立和組裝模組的基礎,乙個模組能夠依賴於另乙個模組。在通常情況下,乙個程式集就可以看成是乙個模組。在abp框架中,乙個模組通過乙個類來定義,而這個類要繼承自abpmodule。模組系統當前專注於服務端而不是客戶端。譯者注 如果學習過orchard的朋友,應該知道module模組的強大...