MZNetwork.AutoDependency.CodeGen
1.2.1
dotnet add package MZNetwork.AutoDependency.CodeGen --version 1.2.1
NuGet\Install-Package MZNetwork.AutoDependency.CodeGen -Version 1.2.1
<PackageReference Include="MZNetwork.AutoDependency.CodeGen" Version="1.2.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="MZNetwork.AutoDependency.CodeGen" Version="1.2.1" />
<PackageReference Include="MZNetwork.AutoDependency.CodeGen"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add MZNetwork.AutoDependency.CodeGen --version 1.2.1
#r "nuget: MZNetwork.AutoDependency.CodeGen, 1.2.1"
#:package MZNetwork.AutoDependency.CodeGen@1.2.1
#addin nuget:?package=MZNetwork.AutoDependency.CodeGen&version=1.2.1
#tool nuget:?package=MZNetwork.AutoDependency.CodeGen&version=1.2.1
MZNetwork.AutoDependency.CodeGen
Roslyn Incremental Source Generator for automatic dependency injection wiring in .NET applications.
MZNetwork.AutoDependency.CodeGen scans your project during compilation for types annotated with [AutoDependency], automatically builds a topological dependency graph, and generates compile-time extension methods to register all your services with zero runtime reflection overhead.
Features
- ⚡ Zero Reflection Overhead: Generates pure C# compile-time registrations directly calling
IServiceCollectionextension methods. - 🔄 Topological Dependency Sorting: Analyzes constructor dependencies and registers services in the exact order needed to satisfy dependencies.
- 🎯 Flexible Lifetimes: Full support for
Transient(default),Scoped, andSingletonservice lifetimes. - 🔑 Keyed Services Support: Built-in support for .NET Keyed service registrations (
AddKeyedSingleton,AddKeyedScoped,AddKeyedTransient). - 🧩 Auto & Explicit Interface Mapping: Inferred interface mapping or explicit service and parent interface registrations.
- 🏭 Factory Pattern Support: Automatically wires factory classes and factory-produced instances.
- 📦 Zero Runtime Dependencies: Attributes and helper interfaces are injected directly into your compilation during post-initialization.
Installation
Install the package via the .NET CLI:
dotnet add package MZNetwork.AutoDependency.CodeGen
Or add the package reference directly to your .csproj:
<ItemGroup>
<PackageReference Include="MZNetwork.AutoDependency.CodeGen" Version="1.2.0" PrivateAssets="all" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Quick Start
1. Annotate Your Services with [AutoDependency]
using MZNetwork.AutoDependency;
namespace MyApp.Services;
// 1. Basic registration (Interface automatically inferred as IOrderService with Transient lifetime)
public interface IOrderService { void ProcessOrder(); }
[AutoDependency]
public class OrderService : IOrderService
{
public void ProcessOrder() { }
}
// 2. Explicit Singleton lifetime
public interface ICacheService { }
[AutoDependency(ServiceLifetime.Singleton)]
public class CacheService : ICacheService { }
// 3. Keyed Dependency registration (.NET 8+)
public interface INotificationService { }
[AutoDependency("emailService", ServiceLifetime.Scoped)]
public class EmailNotificationService : INotificationService { }
// 4. Multiple / Parent Interface mapping (registers IParentService -> OrderHandler)
public interface IParentService { }
public interface IOrderHandler { }
[AutoDependency(typeof(IOrderHandler), typeof(IParentService))]
public class OrderHandler : IOrderHandler, IParentService { }
// 5. Factory pattern (registers MyFactory AND the returned Product)
public class Product { }
[AutoDependency(typeof(MyFactory), ServiceLifetime.Singleton)]
public class MyFactory
{
public Product Create()
{
return new Product();
}
}
2. Configure Service Registration at Startup
In your Program.cs (ASP.NET Core, Console, Worker, etc.):
using Microsoft.Extensions.DependencyInjection;
using MyApp; // Target project root namespace
var builder = WebApplication.CreateBuilder(args);
// Automatically register all [AutoDependency] annotated services
builder.Services.AutoConfigureDependencies();
var app = builder.Build();
app.Run();
You can also invoke the static resolver directly if preferred:
DependencyResolver.AutoConfigureDependencies(builder.Services);
Attribute Constructor Reference
[AutoDependency] provides flexible constructors and properties to suit any registration pattern:
| Attribute Usage | Lifetime | Description |
|---|---|---|
[AutoDependency] |
Transient |
Interface automatically inferred from implemented interfaces |
[AutoDependency(typeof(IService))] |
Transient |
Explicit service interface |
[AutoDependency(typeof(IService), typeof(IParent))] |
Transient |
Explicit service and parent interface |
[AutoDependency("key")] |
Transient |
Keyed dependency with inferred interface |
[AutoDependency(typeof(IService), "key")] |
Transient |
Explicit service type and key |
[AutoDependency(ServiceLifetime.Singleton)] |
Explicit | Explicit lifetime (Singleton, Scoped, or Transient) |
[AutoDependency("key", ServiceLifetime.Scoped)] |
Explicit | Keyed dependency with explicit lifetime |
[AutoDependency(typeof(IService), "key", ServiceLifetime.Singleton)] |
Explicit | Explicit service, key, and lifetime |
[AutoDependency(typeof(IService), typeof(IParent), "key", ServiceLifetime.Scoped)] |
Explicit | Service, parent interface, key, and lifetime |
[AutoDependency(typeof(IService), typeof(IParent), ServiceLifetime.Singleton)] |
Explicit | Service, parent interface, and lifetime |
Named Parameters
You can also use named parameters for clarity:
[AutoDependency(ServiceType = typeof(IMyService), Key = "primary", Lifetime = ServiceLifetime.Singleton)]
public class MyService : IMyService { }
Target Frameworks & Compatibility
- Analyzer Execution: Roslyn Incremental Generator (.NET Standard 2.0 / 2.1)
- Target Projects: .NET 8.0+, .NET 9.0+, .NET 10.0+, .NET Standard 2.0+
- Compatible Application Types: ASP.NET Core, Worker Services, Console Applications, Class Libraries, WPF, Windows Forms, etc.
License
MIT / Proprietary (MZNetwork)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.