DigitalRuby.SimpleDi
1.0.0
See the version list below for details.
dotnet add package DigitalRuby.SimpleDi --version 1.0.0
NuGet\Install-Package DigitalRuby.SimpleDi -Version 1.0.0
<PackageReference Include="DigitalRuby.SimpleDi" Version="1.0.0" />
paket add DigitalRuby.SimpleDi --version 1.0.0
#r "nuget: DigitalRuby.SimpleDi, 1.0.0"
// Install DigitalRuby.SimpleDi as a Cake Addin #addin nuget:?package=DigitalRuby.SimpleDi&version=1.0.0 // Install DigitalRuby.SimpleDi as a Cake Tool #tool nuget:?package=DigitalRuby.SimpleDi&version=1.0.0
SimpleDi
Declarative Dependency Injection and Configuration for .NET
SimpleDi allows you to inject interfaces and types using attributes. No need for complex frameworks or manually adding injections to your startup code.
Setup:
using DigitalRuby.SimpleDi;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSimpleDi();
Implementation:
Create a class, MyInterfaceImplementation
// Interface will be available in constructors
public interface IMyInterface
{
}
// bind the class to simpledi, implementing IMyInterface
[Binding(BindingType.Singleton)]
public sealed class MyInterfaceImplementation : IMyInterface
{
}
Inject the interface into the constructor of another class:
[Binding(BindingType.Singleton)]
public sealed class MyClass
{
public MyClass(IMyInterface myInterface)
{
}
}
Simple di takes care of hosted services too.
[Binding(BindingType.Singleton)]
public sealed class MyHostedClass : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
You can choose whether to bind no interfaces or only some interfaces:
// only register MyClass concrete type
[Binding(BindingType.Singleton, null)]
public sealed class MyClass1 : IInterface1, IInterface2
{
}
// only register MyClass concrete type and IInterface1
[Binding(BindingType.Singleton, typeof(IInterface1))]
public sealed class MyClass2 : IInterface1, IInterface2
{
}
Configuration:
Given a json file in your project config.json
(set as content and copy newer in properties):
{
"DigitalRuby.SimpleDi.Tests.Configuration":
{
"Value": "hellothere"
}
}
And a class with the same namespace as in the file...
namespace DigitalRuby.SimpleDi.Tests;
/// <summary>
/// Config class that binds to key DigitalRuby.SimpleDi.Tests.Configuration
/// </summary>
[Configuration]
public sealed class Configuration
{
/// <summary>
/// Example value
/// </summary>
public string Value { get; set; } = string.Empty; // overriden from config
}
You can inject your Configuration
class into any constructor as normal.
Make sure to add your config file to your configuration builder:
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddJsonFile("config.json");
You can create multiple keys in your configuration file for each class annotated with the Configuration
attribute, or use separate files.
Instead of custom files you can also just use your appsettings.json
file, which is added by .NET automatically.
Thank you for visiting!
-- Jeff
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DigitalRuby.SimpleDi:
Package | Downloads |
---|---|
DigitalRuby.SimplePubSub
Declarative and simple pub/sub for .NET using Mass Transit. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.1 | 152 | 9/15/2024 |
2.0.0 | 225 | 12/29/2023 |
1.0.22 | 265 | 3/21/2023 |
1.0.21 | 272 | 3/10/2023 |
1.0.20 | 258 | 3/9/2023 |
1.0.19 | 256 | 3/6/2023 |
1.0.18 | 242 | 3/6/2023 |
1.0.17 | 252 | 3/5/2023 |
1.0.16 | 264 | 3/5/2023 |
1.0.15 | 259 | 3/5/2023 |
1.0.14 | 259 | 3/5/2023 |
1.0.13 | 244 | 3/5/2023 |
1.0.12 | 242 | 3/5/2023 |
1.0.11 | 460 | 7/3/2022 |
1.0.10 | 426 | 5/31/2022 |
1.0.8 | 871 | 5/26/2022 |
1.0.7 | 441 | 5/26/2022 |
1.0.6 | 446 | 5/25/2022 |
1.0.5 | 421 | 5/25/2022 |
1.0.4 | 418 | 5/25/2022 |
1.0.3 | 424 | 5/25/2022 |
1.0.2 | 441 | 5/21/2022 |
1.0.1 | 420 | 5/21/2022 |
1.0.0 | 407 | 5/20/2022 |
Initial release