ABP框架系列之三十三:(Module-System-模块系统)
Introduction
ASP.NET Boilerplate provides an infrastructure to build modules and compose them to create an application. A module can depend on another module. Generally, an assembly is considered as a module. If you created an application with more than one assembly, it's suggested to create a module definition for each assembly.
Module system is currently focused on server side rather than client side.
ASP.NET的模板提供了一个基础设施建设模块和它们组合来创建一个应用程序。模块可以依赖于另一个模块。一般来说,程序集被认为是一个模块。如果使用多个程序集创建应用程序,建议为每个程序集创建一个模块定义。
模块系统目前主要集中在服务器端而不是客户端。
Module Definition
A module is defined with a class that is derived from AbpModule. Say that we're developing a Blog module that can be used in different applications. Simplest module definition can be as shown below:
一个模块是一个来自abpmodule类定义。比如说我们正在开发一个可以在不同应用程序中使用的博客模块。最简单的模块定义可以如下所示:
public class MyBlogApplicationModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
Module definition class is responsible to register it's classes to dependency injection (can be done conventionally as shown above) if needed. It can configure application and other modules, add new features to the application and so on...
模块定义类负责将它的类注册到依赖注入(如果需要,可以按上面的方式进行常规处理)。它可以配置应用程序和其他模块,向应用程序添加新功能,等等…
Lifecycle Methods
ASP.NET Boilerplate calls some specific methods of modules on application startup and shutdown. You can override these methods to perform some specific tasks.
ASP.NET Boilerplate calls these methods ordered by dependecies. If module A depends on module B, module B is initialized before module A. Exact order of startup methods: PreInitialize-B, PreInitialize-A, Initialize-B, Initialize-A, PostInitialize-B and PostInitialize-A. This is true for all dependency graph. Shutdown method is also similar but in reverse order.
ASP.NET样板要求对应用程序启动和关闭模块的一些具体方法。您可以重写这些方法来执行特定的任务。
ASP.NET的模板调用这些方法由依赖关系。如果模块依赖模块B,B在A模块模块顺序启动方法初始化:preinitialize-b,preinitialize-a,initialize-b,initialize-a,postinitialize-b和postinitialize-a.这是真正的所有依赖关系图。关闭方法也类似,但顺序相反。
PreInitialize
This method is called first when application starts. It's usual method to configure the framework and other modules before they initialize.
Also, you can write some specific code here to run before dependency injection registrations. For example, if you create a conventional registration class, you should register it here using IocManager.AddConventionalRegisterer method.
应用程序启动时首先调用此方法。通常是在初始化之前配置框架和其他模块的方法。
另外,您可以在这里写一些特定的代码在依赖注入注册之前运行。例如,如果你创建一个传统的注册类,你应该在这里登记,使用iocmanager.addconventionalregisterer方法。
Initialize
It's the usual place where dependency injection registration should be done. It's generally done using IocManager.RegisterAssemblyByConvention method. If you want to define custom dependency registration, see dependency injection documentation.
这通常是进行依赖注入注册的地方。这是一般使用iocmanager.registerassemblybyconvention方法。如果要定义自定义依赖项注册,请参阅依赖注入文档。
PostInitialize
This method is called lastly in startup progress. It's safe to resolve a dependency here.
这种方法在启动过程中称为最后。在这里解决依赖是安全的。
Shutdown
This method is called when the application shuts down.
Module Dependencies
A module can be dependent to another. It's required to explicitly declare dependencies using DependsOn attribute like below:
模块可以依赖于另一个模块。需要显式声明的依赖性取决于属性如下:
[DependsOn(typeof(MyBlogCoreModule))]
public class MyBlogApplicationModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
Thus, we declare to ASP.NET Boilerplate that MyBlogApplicationModule depends on MyBlogCoreModule and the MyBlogCoreModule should be initialized before the MyBlogApplicationModule.
ABP can resolve dependencies recursively beginning from the startup module and initialize them accordingly. Startup module initialized as the last module.
因此,我们宣布ASP.NET的样板,MyBlogApplicationModule取决于myblogcoremodule ,MyBlogCoreModule应该在MyBlogApplicationModule之前的初始化。
ABP可以递归地从启动模块开始解析依赖关系,并相应地初始化它们。启动模块作为最后一个模块初始化。
PlugIn Modules(插件模块)
While modules are investigated beginning from startup module and going to dependencies, ABP can also load modules dynamically. AbpBootstrapper class defines PlugInSources property which can be used to add sources to dynamically load plugin modules. A plugin source can be any class implements IPlugInSource interface. PlugInFolderSource class implements it to get plugin modules from assemblies located in a folder.
模块从启动模块开始到依赖性的研究,ABP也可以动态加载模块。abpbootstrapper类定义了pluginsources属性可用于添加源动态加载的插件模块。一个插件的源可以是任何类实现ipluginsource接口。pluginfoldersource类实现了它从位于文件夹组件得到的插件模块。
ASP.NET Core
ABP ASP.NET Core module defines options in AddAbp extension method to add plugin sources in Startup class:
services.AddAbp<MyStartupModule>(options =>
{
options.PlugInSources.Add(new FolderPlugInSource(@"C:\MyPlugIns"));
});
We could use AddFolder extension method for a simpler syntax:
services.AddAbp<MyStartupModule>(options =>
{
options.PlugInSources.AddFolder(@"C:\MyPlugIns");
});
See ASP.NET Core document for more on Startup class.
ASP.NET MVC, Web API
For classic ASP.NET MVC applications, we can add plugin folders by overriding Application_Start in global.asax as shown below:
public class MvcApplication : AbpWebApplication<MyStartupModule>
{
protected override void Application_Start(object sender, EventArgs e)
{
AbpBootstrapper.PlugInSources.AddFolder(@"C:\MyPlugIns");
//...
base.Application_Start(sender, e);
}
}
Controllers in PlugIns
If your modules include MVC or Web API Controllers, ASP.NET can not investigate your controllers. To overcome this issue, you can change global.asax file like below:
using System.Web;
using Abp.PlugIns;
using Abp.Web;
using MyDemoApp.Web; [assembly: PreApplicationStartMethod(typeof(PreStarter), "Start")] namespace MyDemoApp.Web
{
public class MvcApplication : AbpWebApplication<MyStartupModule>
{
} public static class PreStarter
{
public static void Start()
{
//...
MvcApplication.AbpBootstrapper.PlugInSources.AddFolder(@"C:\MyPlugIns\");
MvcApplication.AbpBootstrapper.PlugInSources.AddToBuildManager();
}
}
}
Additional Assemblies(附加组件)
Default implementations for IAssemblyFinder and ITypeFinder (which is used by ABP to investigate specific classes in the application) only finds module assemblies and types in those assemblies. We can overrideGetAdditionalAssemblies method in our module to include additional assemblies.
对于iassemblyfinder和itypefinder默认实现(这是用ABP调查在特定应用类)只发现模块的程序集和类型的组件。我们可以overridegetadditionalassemblies方法在我们的模块,包括额外的组件。
Custom Module Methods(自定义模块的方法)
Your modules also can have custom methods those can be used by other modules depend on this module. Assume that MyModule2 depends on MyModule1 and wants to call a method of MyModule1 in PreInitialize.
public class MyModule1 : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
} public void MyModuleMethod1()
{
//this is a custom method of this module
}
} [DependsOn(typeof(MyModule1))]
public class MyModule2 : AbpModule
{
private readonly MyModule1 _myModule1; public MyModule2(MyModule1 myModule1)
{
_myModule1 = myModule1;
} public override void PreInitialize()
{
_myModule1.MyModuleMethod1(); //Call MyModule1's method
} public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
Here, we constructor-injected MyModule1 to MyModule2, so MyModule2 can call MyModule1's custom method. This is only possible if Module2 depends on Module1.
Module Configuration
While custom module methods can be used to configure modules, we suggest to use startup configuration system to define and set configuration for modules.
Module Lifetime
Module classes are automatically registered as singleton.
ABP框架系列之三十三:(Module-System-模块系统)的更多相关文章
- ABP框架系列之三十四:(Multi-Tenancy-多租户)
What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...
- ABP框架系列之四十三:(OData-Integration-OData集成)
Introduction OData is defined as "An open protocol to allow the creation and consumption of que ...
- ABP框架系列之三十一:(Localization-本地化)
Introduction Any application has at least one language for user interface. Many applications have mo ...
- ABP框架系列之十三:(Authorization-授权)
Introduction Almost all enterprise applications use authorization in some level. Authorization is us ...
- ABP框架系列之三:(Entity Framework Integration-实体框架集成)
ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with EntityFramewo ...
- ABP框架系列之三十七:(Navigation-导航)
Every web application has some menu to navigate between pages/screens. ASP.NET Boilerplate provides ...
- ABP框架系列之五十三:(Web-API-Controllers-Web-API-控制器)
Introduction ASP.NET Boilerplate is integrated to ASP.NET Web API Controllers via Abp.Web.Api nuget ...
- ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
Introduction ASP.NET Boilerplate is integrated to ASP.NET MVC Controllers via Abp.Web.Mvc nuget pack ...
- ABP框架系列之三十八:(NHibernate-Integration-NHibernate-集成)
ASP.NET Boilerplate can work with any O/RM framework. It has built-in integration with NHibernate. T ...
随机推荐
- ssh免密钥之上厕所
ssh服务简单介绍 SSH协议框架中最主要的部分是三个协议: *传输层协议(The Transport Layer Protocol)提供服务器认证,数据机密性,信息完整性等的支持; *用户认证协议( ...
- Linux常用命令大全(分类)
首先按ESC键回到命令模式: vi保存文件有不同的选项,对应于不同的命令,你可以从下面的命令中选择一个需要的::w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi:w! ...
- 【Noip模拟 20160929】选数
题目描述 现在有一排共N个数,你需要从中选出恰好K个.选出K个数后,计算它们两两差值的绝对值的最小值S.你需要确定选出哪K个,才能最大化这个S. 输入数据 输入第一行两个正整数N.K,含义如上. 输入 ...
- K8s部署使用CFSSL创建证书
证书的编码格式 PEM(Privacy Enhanced Mail),通常用于数字证书认证机构(Certificate Authorities,CA),扩展名为.pem, .crt, .cer, 和 ...
- thinkphp5.0 - Redis 实现秒杀
首先,因为秒杀这个环节在商城项目中比较常见,最近写商城项目,碰到这个功能模块,于是就拿出来给大家分享一波. 难点:高并发的情况下,正常逻辑写的话数据库的库存会出现负数,对付这类问题有很多解决方案,我就 ...
- ImportError: No module named pycocotools.coco,pycocotools/_mask.so: undefined symbol: _Py_ZeroStruct
准确的说是没有安装 pycocotools 可以借鉴下面链接: https://blog.csdn.net/ab0902cd/article/details/79085797 因为我通常用Python ...
- Ubuntu下解决MySQL自启动,chkconfig list 全部off 情况
chkconfig命令是用于RedHat/Fedora发行版的,而对于像Ubuntu之类的Debian发行版,应该使用这个命令: sudo update-rc.d mysql defaults 验证一 ...
- 如何在 ajax 外拿到 ajax 的数据???和ajax的参数
第一步: var 变量名 = $.ajax({ url: "发送请求的地址", dataType: 'json', type: 'post', async: false }) 第 ...
- python中使用Opencv进行车牌号检测——2018.10.24
初学Python.Opencv,想用它做个实例解决车牌号检测. 车牌号检测需要分为四个部分:1.车辆图像获取.2.车牌定位.3.车牌字符分割和4.车牌字符识别 在百度查到了车牌识别部分车牌定位和车牌字 ...
- cdnbest里站点域名不同步到节点,报400错误的一般原因
报400错误一般是站点里的域名没有同步到节点上面的原因,产生的原因一般是下面两点原因: 1.检查节点列表如下图所示的状态是否打钩,这是节点和主控的通信状态,打叉表示连接有问题 这里打叉的几种原因(1) ...