DIAutowire 1.0.4
dotnet add package DIAutowire --version 1.0.4
NuGet\Install-Package DIAutowire -Version 1.0.4
<PackageReference Include="DIAutowire" Version="1.0.4" />
<PackageVersion Include="DIAutowire" Version="1.0.4" />
<PackageReference Include="DIAutowire" />
paket add DIAutowire --version 1.0.4
#r "nuget: DIAutowire, 1.0.4"
#:package DIAutowire@1.0.4
#addin nuget:?package=DIAutowire&version=1.0.4
#tool nuget:?package=DIAutowire&version=1.0.4
DIAutowire
A source-generator-powered dependency injection autowiring library for .NET that removes boilerplate from DI registration and startup sequencing.
Features
[Autowire]— Source-generates DI registrations automatically[DeclareDIComponentType]— Generates custom component-type attributes (e.g.[Service],[Repository])[DIComponent]/ built-in types — Triggers automatic interface generation (IClassName)[Autorun]— Marks methods for execution at startup with priority controlServiceResolver<T>— Helper for resolving services with named and scoped support
Getting Started
Reference both DIAutowire and DISourceGenerator (as an Analyzer) in your project:
<ItemGroup>
<ProjectReference Include="..\DIAutowire.csproj" />
<ProjectReference Include="..\DISourceGenerator\DISourceGenerator.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Register all autowired services and launch autorun methods in Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.DIAutowire(); // registers all [Autowire]-marked classes
var app = builder.Build();
await app.Services.LaunchAutorun(); // runs all [Autorun]-marked methods
app.Run();
Autowiring
[Autowire]
Place on any partial class to register it with DI. The source generator creates an IClassName interface and emits an AddScoped<IClassName, ClassName>() (or other lifetime) call.
[Autowire] // Scoped (default)
[Autowire(ServiceLifetime.Singleton)] // Singleton
[Autowire(ServiceLifetime.Transient)] // Transient
[Autowire("my-impl")] // Named (keyed) — AddKeyedScoped
[Autowire("my-impl", ServiceLifetime.Singleton)] // Named + lifetime
The class must be partial for the generated IClassName interface to be wired in automatically.
/// <summary>
/// Processes payment transactions.
/// </summary>
[Autowire(ServiceLifetime.Scoped)]
public partial class PaymentService
{
public decimal ProcessPayment(decimal amount) => amount * 0.98m;
}
The generator produces:
// <auto-generated/>
/// <summary>
/// Processes payment transactions.
/// </summary>
public interface IPaymentService
{
decimal ProcessPayment(decimal amount);
}
public partial class PaymentService : IPaymentService { }
XML doc comments from the class and its members are automatically inherited by the generated interface.
Component Types
Built-in types
Three component-type attributes are included out of the box:
| Attribute | Inherits |
|---|---|
[Service] |
DIComponentAttribute |
[Component] |
DIComponentAttribute |
[Repository] |
DIComponentAttribute |
These are semantic markers. When combined with [Autowire], the class is both labelled and registered.
[Service]
[Autowire(ServiceLifetime.Scoped)]
public partial class OrderService { ... }
[DeclareDIComponentType]
Declare a custom component type at the assembly level. The source generator creates a new attribute class inheriting from DIComponentAttribute.
// AssemblyInfo.cs or Program.cs
[assembly: DeclareDIComponentType("Adapter")]
[assembly: DeclareDIComponentType("Gateway")]
This generates:
// <auto-generated/>
public class AdapterAttribute : DIComponentAttribute { }
public class GatewayAttribute : DIComponentAttribute { }
You can then use them just like the built-in types:
[Adapter]
[Autowire(ServiceLifetime.Transient)]
public partial class HttpGatewayAdapter { ... }
Autorun
[Autorun]
Marks a method to be automatically invoked during LaunchAutorun(). Discovered via reflection at startup.
[Autowire(ServiceLifetime.Singleton)]
public partial class CacheWarmupService
{
[Autorun(AutorunPriority.Critical)]
public async Task WarmupCaches()
{
// runs before anything else
}
[Autorun(AutorunPriority.Low)]
public void LogStartup()
{
Console.WriteLine("Service started.");
}
}
Methods may be void, Task, or ValueTask. Both sync and async methods are supported.
AutorunPriority
| Value | Order |
|---|---|
Critical = 3 |
First |
High = 2 |
Second |
Default = 1 |
Third |
Low = 0 |
Last |
Singleton services are resolved from the root provider. Scoped and Transient services run inside a temporary scope that is auto-disposed after all autoruns complete.
LaunchAutorun()
Extension method on IServiceProvider:
await app.Services.LaunchAutorun();
ServiceResolver<T>
A helper injectable class for resolving DI components with flexible strategies.
Registered automatically by DIAutowire() via AddTransient(typeof(ServiceResolver<>)).
[Autowire(ServiceLifetime.Singleton)]
public partial class OrderProcessor
{
private readonly IPaymentService _payment;
public OrderProcessor(ServiceResolver<IPaymentService> resolver)
{
// Resolve the default (unkeyed) implementation
_payment = resolver.Resolve();
// Resolve a named (keyed) implementation
_payment = resolver.Resolve("stripe");
// Resolve with a new auto-disposable scope
using var scoped = resolver.ResolveScoped();
scoped.Component.ProcessPayment(100m);
// Resolve a named implementation in a new scope
using var namedScoped = resolver.ResolveScoped("stripe");
namedScoped.Component.ProcessPayment(100m);
}
}
ScopedResult<T>
Returned by ResolveScoped(). Holds the resolved component and disposes the scope when Dispose() is called. Use with using statements for correct lifetime management.
using var result = resolver.ResolveScoped("my-key");
var value = result.Component.DoWork();
// scope is disposed here
Project Structure
DIAutowire/
├── Attributes/
│ ├── Implementation/
│ │ └── AutowireAttribute.cs # [Autowire] — registers classes to DI
│ ├── Interface/
│ │ ├── DIComponentAttribute.cs # Base for component-type markers
│ │ └── DeclareDIComponentType.cs # [DeclareDIComponentType] — generates new marker attrs
│ └── Method/
│ └── AutorunAttribute.cs # [Autorun] — marks methods for startup execution
├── Core/
│ └── BuiltInDIComponentTypes.cs # Declares built-in [Service], [Component], [Repository]
├── DISourceGenerator/
│ └── DIAutowireGenerator.cs # Roslyn incremental source generator
├── Enums/
│ └── AutorunPriority.cs # Low / Default / High / Critical
├── Extensions/
│ ├── IApplicationBuilderExtensions.cs # LaunchAutorun() extension
│ └── SemaphoreSlimExtensions.cs # Async locking helper
├── Helpers/
│ ├── ServiceResolver.cs # ServiceResolver<T> + ScopedResult<T>
│ └── SemaphoreDisposer.cs # Semaphore RAII helper
└── DIAutowire.Tests/ # xUnit test project
├── AutowireTests.cs
├── LaunchAutorunTests.cs
└── ServiceResolverTests.cs
Requirements
- .NET 8.0+ (library), .NET 10.0 (tests)
Microsoft.Extensions.DependencyInjection.Abstractions≥ 8.0.2
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- JetBrains.Annotations (>= 2025.2.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.