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
<PackageReference Include="omy.Utils.DependencyInjection" Version="1.2.1" />
<PackageVersion Include="omy.Utils.DependencyInjection" Version="1.2.1" />
<PackageReference Include="omy.Utils.DependencyInjection" />
paket add omy.Utils.DependencyInjection --version 1.2.1
#r "nuget: omy.Utils.DependencyInjection, 1.2.1"
#:package omy.Utils.DependencyInjection@1.2.1
#addin nuget:?package=omy.Utils.DependencyInjection&version=1.2.1
#tool nuget:?package=omy.Utils.DependencyInjection&version=1.2.1
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 | Versions 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. |
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
- omy.Utils (>= 1.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.