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. 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:
public class MyBlogApplicationModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
ASP.NET Boilerplate scans all assemblies and finds all classes those are devired from AbpModule class. If you created an application with more than one assembly, it's suggested to create a module definition for each assembly.
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.
This method is called first when application starts. 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). You can register to events of IOC container.. so on.
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.
This method is called lastly in startup progress. It's safe to resolve a dependency here.
This method is called when the application shuts down.
A module can be dependent to another. ASP.NET Boilerplate tries to resolve these dependencies automatically. But it's suggested 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 is depended to MyBlogCoreModule and the core module should be initialized before the application module.
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 event.
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.