1. 程式人生 > 其它 >ASP.NET Zero筆記(模組系統)

ASP.NET Zero筆記(模組系統)

簡介

在ABP中, 模板的定義就是一個類, 只需要繼承 AbpModule, 該類可以通過nuget包搜尋 ABP 安裝。

下面演示在應用程式或類庫中, 定義一個模組:

 public class ApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(ApplicationModule).GetAssembly());
        }
    }

說明: 關於IocManager.RegisterAssemblyByConvention的作用, 則是將當前程式集模組註冊到容器當中, 作為一個模組, 常見的是暴露模組對應的服務,
而其中所有的服務, 都是按照宣告週期而宣告, 例如: ITransientDependency ,ISingletonDependency。

下面展示了IocManager.RegisterAssemblyByConvention 執行的部分細節:

public void RegisterAssembly(IConventionalRegistrationContext context)
{
            //Transient
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ITransientDependency>()
                    .If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
                    .WithService.Self()
                    .WithService.DefaultInterfaces()
                    .LifestyleTransient()
                );

            //Singleton
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ISingletonDependency>()
                    .If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
                    .WithService.Self()
                    .WithService.DefaultInterfaces()
                    .LifestyleSingleton()
                );

            //...
}