omy.Utils.DependencyInjection 1.2.1

dotnet add package omy.Utils.DependencyInjection --version 1.2.1
                    
NuGet\Install-Package omy.Utils.DependencyInjection -Version 1.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="omy.Utils.DependencyInjection" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="omy.Utils.DependencyInjection" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="omy.Utils.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add omy.Utils.DependencyInjection --version 1.2.1
                    
#r "nuget: omy.Utils.DependencyInjection, 1.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package omy.Utils.DependencyInjection@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=omy.Utils.DependencyInjection&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=omy.Utils.DependencyInjection&version=1.2.1
                    
Install as a Cake Tool

Utils.DependencyInjection Library

Utils.DependencyInjection enables attribute-based registration of services with the Microsoft.Extensions.DependencyInjection framework. It targets .NET 9 and automatically adds annotated types to an IServiceCollection.

Usage example

using System.Reflection;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Utils.DependencyInjection;

[Injectable]
public interface IGreetingService { }

[Singleton]
public class GreetingService : IGreetingService { }

var services = new ServiceCollection();
Assembly.GetExecutingAssembly().ConfigureServices(services);
var provider = services.BuildServiceProvider();
var service = provider.GetRequiredService<IGreetingService>();

The generator can also register handlers and their checks:

using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Utils.DependencyInjection;

public class Ping { public bool Valid { get; set; } }

[Singleton]
public class PingCheck : ICheck<Ping, string>
{
    public bool Check(Ping message, List<string> errors)
    {
        if (message.Valid)
        {
            return true;
        }

        errors.Add("invalid");
        return false;
    }
}

[Singleton]
public class PingHandler : IHandler<Ping>
{
    public void Handle(Ping message) => Console.WriteLine("pong");
}

[Singleton]
public class PingCaller : HandlerCaller
{
    public PingCaller(IServiceProvider provider) : base(provider) { }
}

[StaticAuto]
public partial class PingConfigurator : IServiceConfigurator { }

var services = new ServiceCollection();
new PingConfigurator().ConfigureServices(services);
var provider = services.BuildServiceProvider();
var caller = provider.GetRequiredService<IHandlerCaller>();
var errors = new List<CheckError<string>>();
caller.Handle<string>(new Ping { Valid = true }, errors);

Handler system

The library also provides a simple message handler pattern with optional message validation. Multiple handlers and checks can target the same message; every check runs before all handlers execute. When validation fails, each error is associated with the type of the check that produced it.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Utils.DependencyInjection;

public class Ping { }

[Singleton]
public class PingContentCheck : ICheck<Ping, string>
{
    public bool Check(Ping message, List<string> errors)
    {
        return true;
    }
}

[Singleton]
public class PingSecurityCheck : ICheck<Ping, string>
{
    public bool Check(Ping message, List<string> errors)
    {
        return true;
    }
}

[Singleton]
public class PingHandler : IHandler<Ping>
{
    public void Handle(Ping message) => Console.WriteLine("pong");
}

[Singleton]
public class PingLogger : IHandler<Ping>
{
    public void Handle(Ping message) => Console.WriteLine("log");
}

var services = new ServiceCollection();
new Type[] { typeof(PingContentCheck), typeof(PingSecurityCheck), typeof(PingHandler), typeof(PingLogger), typeof(HandlerCaller) }.ConfigureServices(services);
var provider = services.BuildServiceProvider();
var caller = provider.GetRequiredService<IHandlerCaller>();
var errors = new List<CheckError<string>>();
caller.Handle<string>(new Ping(), errors);

Static configuration

The library provides a source generator that implements <code>IServiceConfigurator</code> for classes marked with <code>[StaticAuto]</code>.

using Microsoft.Extensions.DependencyInjection;
using Utils.DependencyInjection;

[Injectable]
public interface IGreetingService { }

[Singleton]
public class GreetingService : IGreetingService { }

[StaticAuto]
public partial class GreetingConfigurator : IServiceConfigurator { }

var services = new ServiceCollection();
new GreetingConfigurator().ConfigureServices(services);
var provider = services.BuildServiceProvider();
var service = provider.GetRequiredService<IGreetingService>();
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.2.1 220 11/3/2025
1.2.0 169 9/26/2025
1.1.1.1 196 8/20/2025