MZNetwork.AutoDependency.Maui.CodeGen
1.1.1
dotnet add package MZNetwork.AutoDependency.Maui.CodeGen --version 1.1.1
NuGet\Install-Package MZNetwork.AutoDependency.Maui.CodeGen -Version 1.1.1
<PackageReference Include="MZNetwork.AutoDependency.Maui.CodeGen" Version="1.1.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="MZNetwork.AutoDependency.Maui.CodeGen" Version="1.1.1" />
<PackageReference Include="MZNetwork.AutoDependency.Maui.CodeGen"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add MZNetwork.AutoDependency.Maui.CodeGen --version 1.1.1
#r "nuget: MZNetwork.AutoDependency.Maui.CodeGen, 1.1.1"
#:package MZNetwork.AutoDependency.Maui.CodeGen@1.1.1
#addin nuget:?package=MZNetwork.AutoDependency.Maui.CodeGen&version=1.1.1
#tool nuget:?package=MZNetwork.AutoDependency.Maui.CodeGen&version=1.1.1
MZNetwork.AutoDependency.Maui.CodeGen
Roslyn Incremental Source Generator for automatic dependency injection wiring in .NET MAUI applications.
MZNetwork.AutoDependency.Maui.CodeGen scans your .NET MAUI project during compilation for types annotated with [AutoDependency], automatically builds a topological dependency graph, and generates fluent MauiAppBuilder and IServiceCollection extension methods to register all your services, view models, and pages with zero runtime reflection overhead.
Features
- 📱 Tailored for .NET MAUI: Includes fluent
MauiAppBuilder.AutoConfigureDependencies()extension method for clean initialization inMauiProgram.cs. - ⚡ Zero Reflection Overhead: Generates pure C# compile-time registrations directly calling
IServiceCollectionmethods. - 🔄 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.
- 🧩 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.Maui.CodeGen
Or add the package reference directly to your MAUI .csproj:
<ItemGroup>
<PackageReference Include="MZNetwork.AutoDependency.Maui.CodeGen" Version="1.1.0" PrivateAssets="all" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Quick Start
1. Annotate Services, ViewModels, and Pages
Annotate your classes with [AutoDependency]:
using MZNetwork.AutoDependency;
namespace MyMauiApp.Services;
// 1. Singleton Service registration
public interface IDatabaseService { }
[AutoDependency(ServiceLifetime.Singleton)]
public class DatabaseService : IDatabaseService { }
// 2. Transient ViewModel registration
[AutoDependency(ServiceLifetime.Transient)]
public class MainViewModel
{
private readonly IDatabaseService _db;
public MainViewModel(IDatabaseService db)
{
_db = db;
}
}
// 3. Transient Page registration (automatically resolved with injected ViewModel)
[AutoDependency]
public class MainPage : Microsoft.Maui.Controls.ContentPage
{
public MainPage(MainViewModel viewModel)
{
BindingContext = viewModel;
}
}
// 4. Keyed Dependency registration
public interface INotificationService { }
[AutoDependency("pushService", ServiceLifetime.Scoped)]
public class PushNotificationService : INotificationService { }
2. Configure Service Registration in MauiProgram.cs
Call the fluent .AutoConfigureDependencies() method on MauiAppBuilder:
using Microsoft.Maui.Hosting;
using MyMauiApp; // Target project root namespace
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.AutoConfigureDependencies() // <-- Fluent call automatically registers all [AutoDependency] types!
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
You can also use the IServiceCollection extension method directly if desired:
builder.Services.AutoConfigureDependencies();
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 MAUI, .NET 9.0 MAUI, .NET 10.0 MAUI
- Platforms: Android, iOS, Mac Catalyst, Windows
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.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.1 | 106 | 8/14/2026 |