Abp framework for implementation-dependent injection is mainly dependent Castle.Core
, in fact, this one should be classified in Castle.Core
the application, if for Castle.Core
knowledge and understanding, you can go to learn under: Castle.Core . Under the more important to analyze the following interface / class:
Core interface
IIocManager
Defined, service registration, service resolution, to judge whether the registration service, as well as the release of the Ioc objects resolved object
IocManager
Implements IIocManager
the interface, where special attention is needed: for long-running programs, such as web, Windows services, and the need to create a large number of dynamic proxy class, then you will need to ensure that the same can be reused ProxyGenerator
, otherwise, it would be around through caching, resulting in CPU utilization and memory consumption is increasing.
In the process of programming directly ProxyGenerator
declare static
can, additionally IocManager
as singletons.
private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator();
IocContainer = new WindsorContainer(new DefaultProxyFactory(ProxyGeneratorInstance))
public static IocManager Instance { get; private set; }
static IocManager()
{
Instance = new IocManager();
}
Constructor static variables ProxyGeneratorInstance
initialized IWindsorContainer IocContainer
, followed by the current object (singleton):
IocContainer.Register(
Component
.For<IocManager, IIocManager, IIocRegistrar, IIocResolver>()
.Instance(this)
);
Register as IocManager
, IIocManager
, IIocRegistrar
, IIocResolver
, so that we parsed by Ioc these objects will return the current instance.
Convention registration
With the improvement of project business complexity, require registration service will be gradually increased, then the management will gradually complicated.
In Abp, the following categories 约定注册
of interfaces: IConventionalDependencyRegistrar, BasicConventionalRegistrar
( )
- ITransientDependency
- ISingletonDependency
- IInterceptor
- IConventionalDependencyRegistrar
ITransientDependency
Transient Services:
Ioc parse a transient service will return when a new transient clients
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly) //从当前上下文程序集中
.IncludeNonPublicTypes() //包含非公共类型
.BasedOn<ITransientDependency>() //ITransientDependency 的实现类
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition) //排除泛型类型
.WithService.Self() //使用服务自身
.WithService.DefaultInterfaces() //注册为默认接口
.LifestyleTransient() //设置生命周期形式为瞬态
);
ISingletonDependency
Singleton Service:
Literal meaning, Ioc embodiment returns a single object that is shared dependency injection vessel, agreed register transient manner very similar:
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<ISingletonDependency>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.WithService.DefaultInterfaces()
.LifestyleSingleton() // 设置生命周期形式为单例
);
IInterceptor
Interceptor:
Sign up for interceptor
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn<IInterceptor>()
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.LifestyleTransient()
);
IConventionalDependencyRegistrar
Dependence convention registration
/// <summary>
/// This interface is used to register dependencies by conventions.
/// </summary>
/// <remarks>
/// Implement this interface and register to <see cref="IocManager.AddConventionalRegistrar"/> method to be able
/// to register classes by your own conventions.
/// </remarks>
public interface IConventionalDependencyRegistrar
{
/// <summary>
/// Registers types of given assembly by convention.
/// </summary>
/// <param name="context">Registration context</param>
void RegisterAssembly(IConventionalRegistrationContext context);
}
In the parameter IConventionalRegistrationContext
contains the current assembly, in IocManager
as well as the necessary registry configuration, which facilitates context we can get through the current container management objects, to complete the current program focused follow the convention of the type to be registered.