什么是Module?

Module就是模块化的设计思想。开发人员可以将自定义的功能以模块的形式集成到项目中。具体的功能也可以设计成一个单独的模块

AbpModule

AbpModule是所有Module的基类。

ABP如何发现Moudle

1. 程序入口调用:AbpBootstrapper.Initialize()

2. 通过入口的Moudle获取到所有互相依赖的Moudle

private static void AddModuleAndDependenciesRecursively(List<Type> modules, Type module)
{
if (!IsAbpModule(module))
{
throw new AbpInitializationException("This type is not an ABP module: " + module.AssemblyQualifiedName);
} if (modules.Contains(module))
{
return;
} modules.Add(module); var dependedModules = FindDependedModuleTypes(module);
foreach (var dependedModule in dependedModules)
{
AddModuleAndDependenciesRecursively(modules, dependedModule);
}
} /// <summary>
/// Finds direct depended modules of a module (excluding given module).
/// </summary>
public static List<Type> FindDependedModuleTypes(Type moduleType)
{
if (!IsAbpModule(moduleType))
{
throw new AbpInitializationException("This type is not an ABP module: " + moduleType.AssemblyQualifiedName);
} var list = new List<Type>(); if (moduleType.GetTypeInfo().IsDefined(typeof(DependsOnAttribute), true))
{
var dependsOnAttributes = moduleType.GetTypeInfo().GetCustomAttributes(typeof(DependsOnAttribute), true).Cast<DependsOnAttribute>();
foreach (var dependsOnAttribute in dependsOnAttributes)
{
foreach (var dependedModuleType in dependsOnAttribute.DependedModuleTypes)
{
list.Add(dependedModuleType);
}
}
} return list;
}

3. 注册所有Moudle

RegisterModules(moduleTypes);

4. 创建所有Module的描述信息

CreateModules(moduleTypes, plugInModuleTypes);
  • AbpModule的基本信息放在AbpModuleInfo类中
  • 多个AbpModuleInfo放在AbpModuleCollection集合中

5. 初始化所有Moudle

public virtual void StartModules()
{
var sortedModules = _modules.GetSortedModuleListByDependency();
sortedModules.ForEach(module => module.Instance.PreInitialize());
sortedModules.ForEach(module => module.Instance.Initialize());
sortedModules.ForEach(module => module.Instance.PostInitialize());
}

由于模块有依赖关系存在,所以初始化之前确定好初始化顺序,即:被依赖的模块要在依赖的模块之前初始化

至此所有Moudle都被Abp框架集成了

如何把自己Moudle中的类和接口注册到abp框架中

比如AbpWebMvcModule这个模块,就是如何把Controller注册到Abp框架中

每个模块都有PreInitialize 和 Initialize方法

  1. 在PreInitialize添加依赖关系
  2. 在Initialize替换ControllerFactory
/// <summary>
/// This module is used to build ASP.NET MVC web sites using Abp.
/// </summary>
[DependsOn(typeof(AbpWebModule))]
public class AbpWebMvcModule : AbpModule
{
/// <inheritdoc/>
public override void PreInitialize()
{
//添加依赖关系
IocManager.AddConventionalRegistrar(new ControllerConventionalRegistrar()); IocManager.Register<IAbpMvcConfiguration, AbpMvcConfiguration>(); Configuration.ReplaceService<IAbpAntiForgeryManager, AbpMvcAntiForgeryManager>();
} /// <inheritdoc/>
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
//用WindsorControllerFactory替换MVC下默认的ControllerFactory
//MVC将使用WindsorControllerFactory从IOC容器中解析出controller
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(IocManager));
HostingEnvironment.RegisterVirtualPathProvider(IocManager.Resolve<EmbeddedResourceVirtualPathProvider>());
}
}

依赖关系表明:注册所有Controller

/// <summary>
/// Registers all MVC Controllers derived from <see cref="Controller"/>.
/// </summary>
public class ControllerConventionalRegistrar : IConventionalDependencyRegistrar
{
/// <inheritdoc/>
public void RegisterAssembly(IConventionalRegistrationContext context)
{
//注册所有Controller
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.BasedOn<Controller>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.LifestyleTransient()
);
}
}

WindsorControllerFactory将替代DefaultControllerFactory

从IOC容器中解析出Controller

/// <summary>
/// This class is used to allow MVC to use dependency injection system while creating MVC controllers.
/// </summary>
public class WindsorControllerFactory : DefaultControllerFactory
{
/// <summary>
/// Reference to DI kernel.
/// </summary>
private readonly IIocResolver _iocManager; /// <summary>
/// Creates a new instance of WindsorControllerFactory.
/// </summary>
/// <param name="iocManager">Reference to DI kernel</param>
public WindsorControllerFactory(IIocResolver iocManager)
{
_iocManager = iocManager;
} /// <summary>
/// Called by MVC system and releases/disposes given controller instance.
/// </summary>
/// <param name="controller">Controller instance</param>
public override void ReleaseController(IController controller)
{
_iocManager.Release(controller);
} /// <summary>
/// Called by MVC system and creates controller instance for given controller type.
/// </summary>
/// <param name="requestContext">Request context</param>
/// <param name="controllerType">Controller type</param>
/// <returns></returns>
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return base.GetControllerInstance(requestContext, controllerType);
} return _iocManager.Resolve<IController>(controllerType);
}
}

模块的生命周期

1. PreInitialize

初始化之前:当应用启动后会首先调用这个方法,在依赖注入之前。

2. Initialize

初始化:这个方法一般是用来依赖注入的,一般调用IocManager.RegisterAssemblyByConvention实现。

3. PostInitialize

用来解析依赖关系。

4. Shutdown

当应用关闭以后,这个方法会被调用。

模块依赖

Abp框架会自动解析模块之间的依赖关系

[DependsOn(typeof(AbpWebCommonModule))]
public class AbpWebModule : AbpModule
{
/// <inheritdoc/>
public override void Initialize()
{
//依赖注入
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}

AbpWebModule依赖于AbpWebCommonModule,AbpWebCommonModule会在AbpWebModule之前初始化

这两个模块启动的顺序为:

AbpWebCommonModule.PreInitialize();
AbpWebModule.PreInitialize(); AbpWebCommonModule.Initialize();
AbpWebModule.Initialize(); AbpWebCommonModule.PostInitialize();
AbpWebModule.PostInitialize();

ABP先完成所有Module的PreInitialize,接着再执行所有Module的Initialize,最后执行PostInitialize。不是执行完一个Module的这三个方法,再去执行下一个Module的这三个方法

调用依赖模块

在AbpWebModule中如何调用AbpWebCommonModule中的方法?

在AbpWebModule中:

private readonly abpWebCommonModule _abpWebCommonModule;

public AbpWebModule(AbpWebCommonModule abpWebCommonModule)
{
_abpWebCommonModule = abpWebCommonModule;
} public override void PreInitialize()
{
//调用abpWebCommonModule中的方法。
//_abpWebCommonModule.
}

abp底层框架的一些功能模块如何注册?

底层的模块都会依赖于AbpKernelModule,AbpKernelModule中主要初始化各种拦截器,如审计日志、多语言、工作单元拦截器等。

public override void PreInitialize()
{
IocManager.AddConventionalRegistrar(new BasicConventionalRegistrar()); IocManager.Register<IScopedIocResolver, ScopedIocResolver>(DependencyLifeStyle.Transient);
IocManager.Register(typeof(IAmbientScopeProvider<>), typeof(DataContextAmbientScopeProvider<>), DependencyLifeStyle.Transient); AddAuditingSelectors();
AddLocalizationSources();
AddSettingProviders();
AddUnitOfWorkFilters();
ConfigureCaches();
AddIgnoredTypes();
}

Reference

ABP源码分析三:ABP Module

ABP-Module的更多相关文章

  1. Spring Boot Starter 和 ABP Module

    Spring Boot 和 ABP 都是模块化的系统,分别是Java 和.NET 可以对比的框架.模块系统是就像乐高玩具一样,一块一块零散积木堆积起一个精彩的世界.每种积木的形状各不相同,功能各不相同 ...

  2. ABP源码分析三:ABP Module

    Abp是一种基于模块化设计的思想构建的.开发人员可以将自定义的功能以模块(module)的形式集成到ABP中.具体的功能都可以设计成一个单独的Module.Abp底层框架提供便捷的方法集成每个Modu ...

  3. ABP源码分析一:整体项目结构及目录

    ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...

  4. ABP 索引

    官方网站 Github ABP集合贴 @ kebinet https://www.codeproject.com/articles/1115763/using-asp-net-core-entity- ...

  5. ABP模块化

    基于Abp模块化.插件化的设计,开发人员可以将自定义的功能以模块的形式集成到项目中. 模块的加载 模块: 插件: 模块及插件的加载路线: 1. 扩展的HttpApplication对象(在Abp.We ...

  6. ABP之模块分析

    本篇作为我ABP介绍的第三篇文章,这次想讲下模块的,ABP文档已经有模块这方面的介绍,但是它只讲到如何使用模块,我想详细讲解下它模块的设计思路. ABP 框架提供了创建和组装模块的基础,一个模块能够依 ...

  7. ABP之模块

    ABP的反射 为什么先讲反射,因为ABP的模块管理基本就是对所有程序集进行遍历,再筛选出AbpModule的派生类,再按照以来关系顺序加载. ABP对反射的封装着重于程序集(Assembly)与类(T ...

  8. 基于DDD的.NET开发框架 - ABP模块设计

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  9. ABP模块设计

    ABP模块设计 返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术 ...

  10. ABP入门系列(15)——创建微信公众号模块

    ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 1. 引言 现在的互联网已不在仅仅局限于网页应用,IOS.Android.平板.智能家居等平台正如 ...

随机推荐

  1. set 利用lower_bound实现key索引

    set中数据类型为结构体T,T中有两个成员key和val定义如下: struct T{ int key,val; T(int k,int v):key(k),val(v){} bool operato ...

  2. (五)solr7.1.0之solrJ的使用

    (五)solr7.1.0之solrJ的使用 下面是solr7的官网API介绍: 网页翻译的不是很准确,只能了解个大概,基本能获取如下信息: 一.构建和运行SolrJ应用程序 对于用Maven构建的项目 ...

  3. 关于Could not resolve dependencies for project

    异常:Could not resolve dependencies for project 思路:网上提出的方案思路都是把相互依赖的项目导入到本地仓库中. 目前一劳永逸的方法是:将<packag ...

  4. Android 软键盘的显示和隐藏,这样操作就对了

    一.前言 如果有需要用到输入的地方,通常会有需要自动弹出或者收起软键盘的需求.开篇明义,本文会讲讲弹出和收起软键盘的一些细节,最终还会从源码进行分析. 想要操作软键盘,需要使用到 InputMetho ...

  5. 重要:关于PPT转图片需要注意的问题

    关于PPT转图片的问题需要注意的问题   我们有一个项目做过直接上传ppt的功能,但是网页不可能显示ppt,所以只能把ppt转成pdf或者图片来显示,我们的做法是转成了图片,然后使用swiper做成类 ...

  6. Linux服务器病毒清理实践

    背景:客户服务器被挂载木马病毒用以挖矿(比特币). 本次清理通过Linux基本命令完成.其原理也比较简单,通过ps命令查看服务器异常进程,然后通过lsof命令定位进程访问的文件,找到异常文件删除之,最 ...

  7. 【java系列】java开发环境搭建

    描述 本篇文章主要讲解基于windows 10系统搭建java开发环境,主要内容包括如下: (1)安装资料准备 (2)安装过程讲解 (3)测试是否安装成功 (4)Hello Word测试 1   安装 ...

  8. 最短路洛谷P2384

    题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你100000000000000000000000000000000000%10金币w ...

  9. php面试之四-Linux部分

    php面试题之四——Linux部分(高级部分) 四.Linux部分 1.请解释下列10个shell命令的用途(新浪网技术部) top.ps.mv.find.df.cat.chmod.chgrp.gre ...

  10. C# 通过反射初探ORM框架的实现原理

    背景: 以前学的Java进行开发,多用到Mybatis,Hiberante等ORM框架,最近需要上手一个C#的项目,由于不是特别难,也不想再去学习C#的ORM框架,所以就想着用反射简单的实现一下ORM ...