Abp dependency injection

From burakdmir/abp-skills · View interactive version

Abp dependency injection is an AI agent skill from the GitHub repository burakdmir/abp-skills. ABP Framework v10.x (10.4–10.6) dependency injection: ITransientDependency/IScopedDependency/ISingletonDependency, [Dependency], [ExposeServices], LazyServiceProvider, property injection, Autofac. Use when you need service registration, DI, or automatic registration in ABP.

How to use this skill

Skill file

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 { }  // Transient
public class CacheService : ISingletonDependency { }   // Singleton
public class RequestTracker : IScopedDependency { }    // Scoped

[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; } // Optional dependency
}

Override Framework Service

[Dependency(ReplaceServices = true)]
public class MyCustomEmailSender : IEmailSender, ITransientDependency { }

Best Practices

Related

Framework · Modularity · DDD · Docs: https://abp.io/docs/latest/framework/fundamentals/dependency-injection

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.