abp模塊生命週期設計思路剖析

abp中將生命週期事件抽象爲4個接口:ide

//預初始化
public interface IOnPreApplicationInitialization
{
    void OnPreApplicationInitialization([NotNull] ApplicationInitializationContext context);
}

//初始化
public interface IOnApplicationInitialization
{
    void OnApplicationInitialization([NotNull] ApplicationInitializationContext context);
}

//初始化完成
public interface IOnPostApplicationInitialization
{
    void OnPostApplicationInitialization([NotNull] ApplicationInitializationContext context);
}

//關閉
public interface IOnApplicationShutdown
{
    void OnApplicationShutdown([NotNull] ApplicationShutdownContext context);
}

abp定義了一個模塊生命週期接口,該接口只有兩個方法,用於初始化和關閉。code

public interface IModuleLifecycleContributor : ITransientDependency
{
    void Initialize([NotNull] ApplicationInitializationContext context, [NotNull] IAbpModule module);

    void Shutdown([NotNull] ApplicationShutdownContext context, [NotNull] IAbpModule module);
}

這個接口的參數是IAbpModule接口的類型,也就是派生自AbpModule的模塊類型。該接口有一個默認實現,是一個抽象類,主要是用於複用。blog

public abstract class ModuleLifecycleContributorBase : IModuleLifecycleContributor
{
    public virtual void Initialize(ApplicationInitializationContext context, IAbpModule module)
    {
    }

    public virtual void Shutdown(ApplicationShutdownContext context, IAbpModule module)
    {
    }
}

abp根據四個生命週期分別有四個類型,這四個類型是基於模塊生命週期基類建立的。接口

public class OnApplicationInitializationModuleLifecycleContributor : ModuleLifecycleContributorBase
{
    public override void Initialize(ApplicationInitializationContext context, IAbpModule module)
    {
        (module as IOnApplicationInitialization)?.OnApplicationInitialization(context);
    }
}

public class OnApplicationShutdownModuleLifecycleContributor : ModuleLifecycleContributorBase
{
    public override void Shutdown(ApplicationShutdownContext context, IAbpModule module)
    {
        (module as IOnApplicationShutdown)?.OnApplicationShutdown(context);
    }
}

public class OnPreApplicationInitializationModuleLifecycleContributor : ModuleLifecycleContributorBase
{
    public override void Initialize(ApplicationInitializationContext context, IAbpModule module)
    {
        (module as IOnPreApplicationInitialization)?.OnPreApplicationInitialization(context);
    }
}

public class OnPostApplicationInitializationModuleLifecycleContributor : ModuleLifecycleContributorBase
{
    public override void Initialize(ApplicationInitializationContext context, IAbpModule module)
    {
        (module as IOnPostApplicationInitialization)?.OnPostApplicationInitialization(context);
    }
}

只有"關閉應用"這個生命週期實現Shutdown方法,其他的都是實現Initialize方法。最後是傳入的參數abp模塊去調用對應的生命週期。
剖析圖以下:
生命週期

相關文章
相關標籤/搜索