Abp Application级别的生命周期

本篇级别: 中高级篇,假设各位知道Abp是什么, Abp里面的基本的概念及用法(想了解基本概念的可在这里学习: http://www.cnblogs.com/mienreal/p/4358806.html

AbpWebApplication

AbpWebApplication是需要我们的Global.cs里面的HttpApplication继承他, 初始化分两个阶段

构造方法AbpWebApplication()

在这个方法里面,做了非常重要但是很简单的事, 通过 AbpBootstrapper类库,间接实例化了IocManager.Instance, 并且注册了IocManager,IIlocManager,IIocRegistrar, IIocResolver做了注册,

也就是这一步完成之后,IocManager.Instance.Reslove只能解析上面的四个类型, 其他类型到目前为止,还一个都没有注册.

 

Application_Start(object sender, EventArgs e)方法

a. 注册了IAssemblyFinder, 关联到了WebAssemblyFinder类库, 猜主要是用来寻找相关类库, 看其代码主要是获取当前bin目录下的所有dll组件,并跟当前已加载的 Assembly做比较,最后返回当前程序已加载的dll, 忽略未加载的.

b. AbpBootstrapper初始化,

b.1 IocManager.IocContainer.Install(new AbpCoreInstaller());
Component.For<IUnitOfWorkDefaultOptions, UnitOfWorkDefaultOptions>().ImplementedBy<UnitOfWorkDefaultOptions>().LifestyleSingleton(),
                Component.For<INavigationConfiguration, NavigationConfiguration>().ImplementedBy<NavigationConfiguration>().LifestyleSingleton(),
                Component.For<ILocalizationConfiguration, LocalizationConfiguration>().ImplementedBy<LocalizationConfiguration>().LifestyleSingleton(),
                Component.For<IAuthorizationConfiguration, AuthorizationConfiguration>().ImplementedBy<AuthorizationConfiguration>().LifestyleSingleton(),
                Component.For<IFeatureConfiguration, FeatureConfiguration>().ImplementedBy<FeatureConfiguration>().LifestyleSingleton(),
                Component.For<ISettingsConfiguration, SettingsConfiguration>().ImplementedBy<SettingsConfiguration>().LifestyleSingleton(),
                Component.For<IModuleConfigurations, ModuleConfigurations>().ImplementedBy<ModuleConfigurations>().LifestyleSingleton(),
                Component.For<IEventBusConfiguration, EventBusConfiguration>().ImplementedBy<EventBusConfiguration>().LifestyleSingleton(),
                Component.For<IMultiTenancyConfig, MultiTenancyConfig>().ImplementedBy<MultiTenancyConfig>().LifestyleSingleton(),
                Component.For<ICachingConfiguration, CachingConfiguration>().ImplementedBy<CachingConfiguration>().LifestyleSingleton(),
                Component.For<IAuditingConfiguration, AuditingConfiguration>().ImplementedBy<AuditingConfiguration>().LifestyleSingleton(),
                Component.For<IAbpStartupConfiguration, AbpStartupConfiguration>().ImplementedBy<AbpStartupConfiguration>().LifestyleSingleton(),
                Component.For<ITypeFinder>().ImplementedBy<TypeFinder>().LifestyleSingleton(),
                Component.For<IModuleFinder>().ImplementedBy<DefaultModuleFinder>().LifestyleTransient(),
                Component.For<IAbpModuleManager>().ImplementedBy<AbpModuleManager>().LifestyleSingleton(),
                Component.For<ILocalizationManager, LocalizationManager>().ImplementedBy<LocalizationManager>().LifestyleSingleton()

上面注册的大部分都是配置类,最后四个有TypeFinder,ModuleFinder,ModuleManager, LocalizationManager.

Navigation: 导航相关,不晓得干啥

LocalizationConfiguration, LocalizationManager, 多语言相关,这个基本根据实际情况看。

Authorization: 授权相关, 跟Authentication有区别,前者授权,后者负责的呢登录认证

Feature: 不晓得干啥

Settings: 看起来是设置相关的

ModuleConfiguration:  Abp模块配置,这个看起来应该很重要,毕竟是搞Module么,这个可是Abp的核心概念

EventBus: 事件总线,看程序规模跟复杂度了,不过是很好玩的概念

MultiTenancy: 多租户概念,也看业务逻辑跟系统应用场景了.

Caching: 缓存,必须得搞清楚的概念

Auditing: 审计,这个设计的还是不错,有很好的参考价值, 但是也有一些弊端,需要综合考虑,感觉利大于弊。

AbpStartup: 管Abp启动阶段的事,上述列的里面,这个是最先用到的,也是最先不用管的:)

TypeFinder: 类型查找的,

ModuleFinder, ModuleManager: 跟上面的ModuleConfiguration一样,都是Module管理相关的

b.2 IocManager.Resolve<AbpStartupConfiguration>().Initialize();
Localization = IocManager.Resolve<ILocalizationConfiguration>();
Modules = IocManager.Resolve<IModuleConfigurations>();
Features = IocManager.Resolve<IFeatureConfiguration>();
Navigation = IocManager.Resolve<INavigationConfiguration>();
Authorization = IocManager.Resolve<IAuthorizationConfiguration>();
Settings = IocManager.Resolve<ISettingsConfiguration>();
UnitOfWork = IocManager.Resolve<IUnitOfWorkDefaultOptions>();
EventBus = IocManager.Resolve<IEventBusConfiguration>();
MultiTenancy = IocManager.Resolve<IMultiTenancyConfig>();
Auditing = IocManager.Resolve<IAuditingConfiguration>();
Caching = IocManager.Resolve<ICachingConfiguration>();

一共11项Configuration, 只是在b.2的介绍里面增加了UnitOfWork的Configuration

UnitOfWork: 工作单元, 这个概念现在很普遍,大家都应该了解.

b.3 _moduleManager.InitializeModules();
public virtual void InitializeModules()
{
    LoadAll();

    var sortedModules = _modules.GetSortedModuleListByDependency();

    sortedModules.ForEach(module => module.Instance.PreInitialize());
    sortedModules.ForEach(module => module.Instance.Initialize());
    sortedModules.ForEach(module => module.Instance.PostInitialize());
}

1. 加载所有Module

2. 按照DepondsOnAttribute对各个Module排序

3. 一次按照PreInit, Init,PostInit的顺序一次执行.

 

Application_End(object sender, EventArgs e)方法

Module Shutdown

var sortedModules = _modules.GetSortedModuleListByDependency();
sortedModules.Reverse();
sortedModules.ForEach(sm => sm.Instance.Shutdown());

这部分很简单,就是通过各个Module的Shutdown释放各个Module的资源

posted @ 2015-11-26 11:57  DukeCheng  阅读(1623)  评论(0编辑  收藏  举报