| name | abp-dependency-injection |
| description | ABP Framework v10.x (10.4/10.5) dependency injection: ITransientDependency/IScopedDependency/ISingletonDependency, [Dependency], [ExposeServices], LazyServiceProvider, property injection, Autofac. Use when you need service registration, DI, or automatic registration in ABP. |
ABP Dependency Injection Skill
Trigger
Dependency injection, DI, ITransientDependency, ISingletonDependency, IScopedDependency, [Dependency], [ExposeServices], auto registration, Autofac.
Quick Reference
Conventional Registration (auto)
public class TaxCalculator : ITransientDependency { }
public class CacheService : ISingletonDependency { }
public class RequestTracker : IScopedDependency { }
[Dependency] Attribute
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)]
public class TaxCalculator { }
[Dependency(ServiceLifetime.Singleton, TryRegister = true)]
public class MyCacheService { }
Properties: Lifetime, TryRegister, ReplaceServices
Higher priority than dependency interfaces.
[ExposeServices] Attribute
[ExposeServices(typeof(ITaxCalculator))]
public class TaxCalculator : ICalculator, ITaxCalculator, ITransientDependency { }
Only ITaxCalculator injectable. Without attribute, all interfaces exposed.
Disable Auto Registration
public class BlogModule : AbpModule
{
public BlogModule() { SkipAutoServiceRegistration = true; }
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAssemblyOf<BlogModule>();
}
}
Autofac (required for dynamic proxying)
dotnet add package Volo.Abp.Autofac
[DependsOn(typeof(AbpAutofacModule))]
public class MyModule : AbpModule { }
Program.cs: builder.Host.UseAutofac();
Property Injection
public class MyService : ITransientDependency
{
public IEmailSender EmailSender { get; set; }
}
Override Framework Service
[Dependency(ReplaceServices = true)]
public class MyCustomEmailSender : IEmailSender, ITransientDependency { }
Best Practices
- ITransientDependency for most services
- Constructor injection preferred
- [Dependency(ReplaceServices=true)] to override framework services
- [ExposeServices] to limit exposed interfaces
- ISingletonDependency only for stateless/caches
- Don't use service locator pattern
- Autofac required for interceptors (UOW, validation, auth, audit)
Related
Framework · Modularity · DDD · Docs: https://abp.io/docs/latest/framework/fundamentals/dependency-injection