【原创】ABP之IConventionalDependencyRegistra接口分析
类图:

作用:
abp中默认把对象的注册分为5中约定注册方式:
1.AbpAspNetCoreConventionalRegistrar
public class AbpAspNetCoreConventionalRegistrar : IConventionalDependencyRegistrar
{
public void RegisterAssembly(IConventionalRegistrationContext context)
{
//ViewComponents
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.BasedOn<ViewComponent>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.LifestyleTransient()
); //PerWebRequest
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<IPerWebRequestDependency>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestyleCustom<MsScopedLifestyleManager>()
);
}
}
2、ApiControllerConventionalRegistrar
/// <summary>
/// Registers all Web API Controllers derived from <see cref="ApiController"/>.
/// </summary>
public class ApiControllerConventionalRegistrar : IConventionalDependencyRegistrar
{
public void RegisterAssembly(IConventionalRegistrationContext context)
{
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.BasedOn<ApiController>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.LifestyleTransient()
);
}
}
3、BasicConventionalRegistrar
/// <summary>
/// This class is used to register basic dependency implementations such as <see cref="ITransientDependency"/> and <see cref="ISingletonDependency"/>.
/// </summary>
public class BasicConventionalRegistrar : IConventionalDependencyRegistrar
{
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>()//如果服务继承了该接口就会自动注册为单例模式,这也就是我们使用ABP时对象自动创建神奇的地方了
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestyleSingleton()
); //Windsor Interceptors
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<IInterceptor>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.LifestyleTransient()
);
}
}
4、ControllerConventionalRegistrar
/// <summary>
/// Registers all MVC Controllers derived from <see cref="Controller"/>.
/// </summary>
public class ControllerConventionalRegistrar : IConventionalDependencyRegistrar
{
/// <inheritdoc/>
public void RegisterAssembly(IConventionalRegistrationContext context)
{
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.BasedOn<Controller>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.LifestyleTransient()
); //PerWebRequest
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<IPerWebRequestDependency>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestylePerWebRequest()
);
}
}
5、FluentValidationValidatorRegistrar
public class FluentValidationValidatorRegistrar : IConventionalDependencyRegistrar
{
public void RegisterAssembly(IConventionalRegistrationContext context)
{
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.BasedOn(typeof(IValidator<>)).WithService.Base()
.LifestyleTransient()
);
}
}
当我们通过IocContainer调用AddConventionalRegistrar(Assembly assembly)这种方式注册时就会分别调用上面5个方法的RegisterAssembly()从assembly中查找是否有符合要求的对象,如果有就把该对象注册
使用场景如下:
[DependsOn(typeof(AbpWebCommonModule))]
public class AbpAspNetCoreModule : AbpModule
{
public override void PreInitialize()
{
IocManager.AddConventionalRegistrar(new AbpAspNetCoreConventionalRegistrar());//assembly调用次数由这里决定,如果5中约定注册方式都被添加了,就会查找5次,如果满足条件就注册 IocManager.Register<IAbpAspNetCoreConfiguration, AbpAspNetCoreConfiguration>(); Configuration.ReplaceService<IPrincipalAccessor, AspNetCorePrincipalAccessor>(DependencyLifeStyle.Transient);
Configuration.ReplaceService<IAbpAntiForgeryManager, AbpAspNetCoreAntiForgeryManager>(DependencyLifeStyle.Transient);
Configuration.ReplaceService<IClientInfoProvider, HttpContextClientInfoProvider>(DependencyLifeStyle.Transient); Configuration.Modules.AbpAspNetCore().FormBodyBindingIgnoredTypes.Add(typeof(IFormFile)); Configuration.MultiTenancy.Resolvers.Add<DomainTenantResolveContributor>();
Configuration.MultiTenancy.Resolvers.Add<HttpHeaderTenantResolveContributor>();
Configuration.MultiTenancy.Resolvers.Add<HttpCookieTenantResolveContributor>();
} public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(AbpAspNetCoreModule).GetAssembly());//此方法调用前必须在PreInitialize()中先调用IocManager.AddConventionalRegistrar才会起作用,否则对象不会被依赖注入
} }
一般在模块注入时使用,相信大家经常这样用
那么为什么会执行5次注入呢?请看下面代码:
/// <summary>
/// Adds a dependency registrar for conventional registration.
/// </summary>
/// <param name="registrar">dependency registrar</param>
public void AddConventionalRegistrar(IConventionalDependencyRegistrar registrar)
{
_conventionalRegistrars.Add(registrar);//这个变量是全局变量,用来当做缓存,如果IocManager. AddConventionalRegistrar()在模块中被调用5次约定注册方式,下面的循环就会跑5次
} /// <summary>
/// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
/// </summary>
/// <param name="assembly">Assembly to register</param>
public void RegisterAssemblyByConvention(Assembly assembly)
{
RegisterAssemblyByConvention(assembly, new ConventionalRegistrationConfig());
} /// <summary>
/// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
/// </summary>
/// <param name="assembly">Assembly to register</param>
/// <param name="config">Additional configuration</param>
public void RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)
{
var context = new ConventionalRegistrationContext(assembly, this, config); foreach (var registerer in _conventionalRegistrars)//循环几次由_conventionalRegistrars决定
{
registerer.RegisterAssembly(context);
} if (config.InstallInstallers)
{
IocContainer.Install(FromAssembly.Instance(assembly));
}
}
这5中约定注册方式是ABP默认的,我们在开发中也可以根据自己的需求扩展自己需要的注入方式
欢迎批评和提建议!
待续.........................................
【原创】ABP之IConventionalDependencyRegistra接口分析的更多相关文章
- 【原创】ABP源码分析
接口篇 IConventionalDependencyRegistra接口分析 待续.............. 模块篇 敬请期待...... 领域篇 敬请期待...... 消息篇 敬请期待..... ...
- 万水千山ABP - 系统发布后迁移 CodeFirst 数据库[原创]
在项目开发的过程中,常会遇到项目发布后还变更数据库的情况.这时如何方便地进行数据库迁移呢 ? 下面直接列出操作的步骤: 1. 发布修改后的应用: 将最新版本的应用更新到目标机器中.更新的文件当然不包括 ...
- JS组件系列——在ABP中封装BootstrapTable
前言:关于ABP框架,博主关注差不多有两年了吧,一直迟迟没有尝试.一方面博主觉得像这种复杂的开发框架肯定有它的过人之处,系统的稳定性和健壮性比一般的开源框架肯定强很多,可是另一方面每每想到它繁琐的封装 ...
- C# ABP源码详解 之 BackgroundJob,后台工作(一)
本文归属作者所有,转发请注明本文链接. 1. 前言 ABP的BackgroundJob,用来处理耗时的操作.比如客户端上传文件,我们要把文件(Excel)做处理,这耗时的操作我们应该放到后台工作者去做 ...
- ABP 数据库 -- ABP&EF中的多表、关联查询
本文介绍一下ABP中的多表查询. 1.创建实体 多表查询,在ABP或者EF中都很简单,这里我们创建一个Demo,一个学生实体.一个学校实体. 学校里面可以有很多学生,学生有一个学校. 实体如下: 学校 ...
- C# ABP 配置连接数据库&创建表
1. 配置连接数据库 配置连接数据库很简单,只需要打开Web项目,然后找到Web.config,配置如下: <connectionStrings> <add name="D ...
- 最美应用API接口分析
最美应用API接口分析html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scr ...
- ABP框架和NET CORE实战
http://www.fishpro.com.cn/2017/09/ ABP实战系列 ABP实战 ABP-第一个Asp.net core 示例(7)AutoMapper的使用 我们为什么需要使用DDD ...
- ABP 切换mysql 数据库报错mysqlexception: incorrect string value: ‘\xe7\xae\x80\xe4\xbd\x93…’ for column display name
刚折腾了ABP框架,为了跨平台,将SQL Server数据库换成了MySQL数据库,ABP框架上支持多语言,中间被字符集折腾的够呛,翻了N个博客,最后终于在StackOverFlow 上找到了最终的解 ...
随机推荐
- Python3学习笔记08-tuple
元组与列表类似,不同之处在于元组的元素不能修改 元组使用小括号,列表使用方括号 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可 tup1 = ('Google', 'Runoob', 19 ...
- Visual Studio 2017 + Python3.6安装scipy库
Windows10下安装scipy很麻烦,直接在命令行下使用pip install scipy无法安装,但可以借助VS2017的集成环境来安装. (1)首先在Visual Studio Install ...
- win32编程:L,_T() ,TEXT和_TEXT
L的使用: 在字符串前面的大写字母L,用来告诉编译器该字符串应该作为Unicode来编译.它用来将ASNI转换为Unicode,Unicode字符串中每个字符占16位(两个字节),而在ASNI中每个字 ...
- 量化投资与Python之NumPy
数组计算 NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础.NumPy的主要功能:ndarray,一个多维数组结构,高效且节省空间无需循环对整组数据进行快速运算的 ...
- 通达OA系统故障解决案例记录
案例1: 现象:在人员访问量大的时候OA系统经卡死,并且经常宕机,需要启动apache服务 优化配置如下: D:\MYOA\conf\http.conf 修改参数如下: <IfModule mp ...
- 转载:编译安装Nginx(1.4)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19617.html 安装Nginx最简单的方式是,进入nginx-1.0.14目录后执行以下3行命令:./configuremakem ...
- java多线程快速入门(四)
通过匿名内部类的方法创建多线程 package com.cppdy; //通过匿名内部类的方法创建多线程 public class ThreadDemo2 { public static void m ...
- Action属性接收参数
一.action的属性(地址栏传参)接收参数:如果使用的JDK属性不一致,则会使得传值无法实现.解决办法:1.系统自身需要用到的JDK(window——>属性——>Java——>In ...
- WebService简介-02
WebService-面向服务编程SOA WebService-远程通信 运行效果: 1:添加服务器引用http://www.webxml.com.cn/WebServices/WeatherWebS ...
- 基于Linux平台的自动化运维Devops-----之自动化系统部署
一.自动化运维的背景网站业务上线,需要运维人员在短时间内完成几百台服务器部署,包括系统安装.系统初始化.软件的安装与配置.性能的监控......所谓运维自动化,即在最少的人工干预下,利用脚本与第三方工 ...