RonSijm.Syringe
0.2.1
Prefix Reserved
dotnet add package RonSijm.Syringe --version 0.2.1
NuGet\Install-Package RonSijm.Syringe -Version 0.2.1
<PackageReference Include="RonSijm.Syringe" Version="0.2.1" />
paket add RonSijm.Syringe --version 0.2.1
#r "nuget: RonSijm.Syringe, 0.2.1"
// Install RonSijm.Syringe as a Cake Addin #addin nuget:?package=RonSijm.Syringe&version=0.2.1 // Install RonSijm.Syringe as a Cake Tool #tool nuget:?package=RonSijm.Syringe&version=0.2.1
RonSijm.Syringe
A library for to make it easier to do dependency injection.
Get it? A Syringe is used to inject things. Yes jokes are better when you have to explain them...
Default ServiceCollection Extensions:
<a id='snippet-CodeExample-DefaultServiceCollection-WireByType'></a>
var serviceCollection = new ServiceCollection();
serviceCollection.WireImplicit(typeof(ClassA));
var serviceProvider = serviceCollection.BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/DefaultServiceCollection/InitiationMethods/TestWireImplicit_Type.cs#L10-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DefaultServiceCollection-WireByType' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-DefaultServiceCollection-WireByTypeGeneric'></a>
var serviceCollection = new ServiceCollection();
serviceCollection.WireImplicit<ClassA>();
var serviceProvider = serviceCollection.BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/DefaultServiceCollection/InitiationMethods/TestWireImplicit_GenericType.cs#L10-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DefaultServiceCollection-WireByTypeGeneric' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-DefaultServiceCollection-WireByAssembly'></a>
var serviceCollection = new ServiceCollection();
var assembly = typeof(ClassA).Assembly;
serviceCollection.WireImplicit(assembly, ServiceLifetime.Transient, []);
var serviceProvider = serviceCollection.BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/DefaultServiceCollection/InitiationMethods/TestWireImplicit_Assembly.cs#L10-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DefaultServiceCollection-WireByAssembly' title='Start of snippet'>anchor</a></sup>
Scoped: (default)
<a id='snippet-CodeExample-DefaultScope'></a>
return typeof(ClassWithGuidA).WireImplicit().BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/Attributes/LifetimeTests/LifetimeTests_Scoped.cs#L10-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DefaultScope' title='Start of snippet'>anchor</a></sup>
Transient:
<a id='snippet-CodeExample-TransientScope'></a>
return typeof(ClassWithGuidA).WireImplicit().WithLifetime(ServiceLifetime.Transient).BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/Attributes/LifetimeTests/LifetimeTests_Transient.cs#L10-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-TransientScope' title='Start of snippet'>anchor</a></sup>
Singleton:
<a id='snippet-CodeExample-SingletonScope'></a>
return typeof(ClassWithGuidA).WireImplicit().WithLifetime(ServiceLifetime.Singleton).BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/Attributes/LifetimeTests/LifetimeTests_Singleton.cs#L10-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-SingletonScope' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-Parameters-registerAsTypesOnly'></a>
var serviceCollection = new ServiceCollection();
serviceCollection.WireImplicit<ClassA>(registerAsTypesOnly: [typeof(ClassWithInterfaceA)]);
var serviceProvider = serviceCollection.BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/DefaultServiceCollection/Parameters/TestWireImplicit_RegisterAsTypesOnly_ClassWithInterface.cs#L11-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-Parameters-registerAsTypesOnly' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-Parameters-registerBothTypeAndInterfaces'></a>
var serviceCollection = new ServiceCollection();
serviceCollection.WireImplicit<ClassA>(registerBothTypeAndInterfaces: [typeof(ClassWithInterfaceA)]);
var serviceProvider = serviceCollection.BuildServiceProvider();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/DefaultServiceCollection/Parameters/TestWireImplicit_RegisterBothTypeAndInterfaces_ClassWithInterface.cs#L10-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-Parameters-registerBothTypeAndInterfaces' title='Start of snippet'>anchor</a></sup>
Extension Methods
By doing a WireImplicit
- The extension will return a SyringeServiceCollectionAndRegistration
object.
This is an object that keeps track of the last registion that you've made, allowing you to append extra configuration to that specific registration:
<a id='snippet-CodeExample-DontRegisterTypesExtension'></a>
var serviceCollection = new SyringeServiceCollection();
serviceCollection.WireImplicit<ClassA>().DontRegisterTypes(typeof(ClassA));
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/CustomCollection/Parameters/TestWireImplicit_DontRegisterAsTypes_Class.cs#L11-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DontRegisterTypesExtension' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-DontRegisterTypesWithInterfaces'></a>
var serviceCollection = new SyringeServiceCollection();
serviceCollection.WireImplicit<ClassA>().DontRegisterTypesWithInterfaces([typeof(InterfaceFor_ClassWithInterfaceA)]);
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/CustomCollection/Parameters/TestWireImplicit_DontRegisterTypesWithInterface_InterfaceFor_ClassWithInterface.cs#L11-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-DontRegisterTypesWithInterfaces' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-RegisterAsTypesOnly'></a>
var serviceCollection = new SyringeServiceCollection();
serviceCollection.WireImplicit<ClassA>().RegisterAsTypesOnly([typeof(ClassWithInterfaceA)]);
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/CustomCollection/Parameters/TestWireImplicit_RegisterAsTypesOnly_ClassWithInterface.cs#L11-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-RegisterAsTypesOnly' title='Start of snippet'>anchor</a></sup>
<a id='snippet-CodeExample-RegisterBothTypeAndInterfaces'></a>
var serviceCollection = new SyringeServiceCollection();
serviceCollection.WireImplicit<ClassA>().RegisterBothTypeAndInterfaces([typeof(ClassWithInterfaceA)]);
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/CustomCollection/Parameters/TestWireImplicit_RegisterBothTypeAndInterfaces_ClassWithInterface.cs#L10-L13' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-RegisterBothTypeAndInterfaces' title='Start of snippet'>anchor</a></sup>
Registration Extensions
A SyringeServiceCollection
contains a internal List<IWireSyringeServiceDescriptorExtension> Extensions { get; set; } = [];
- which lets you add your own BeforeBuildServiceProvider
Extensions
Example
You can put these kind of configurations in your appsettings.json
:
Don't register classes with this interface:
Usecase:
Imagine you have an IProcessor
(Interface), DatabaseProcessor
and APIProcessor
that implement this interface - In your appsettings.json you can register or unregister a specific implementation for this interface.
<a id='snippet-CodeExample-ConfigurationNotWithInterfaces'></a>
var appsetting = """
{
"DependencyInjection": {
"Assembly:RonSijm.Syringe.ExamplesC": {
"Type:ExampleCInterface": "None"
}
}
}
""".ToConfiguration();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/Config/ExamplesC_InterfaceRegistrationNone.cs#L11-L23' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-ConfigurationNotWithInterfaces' title='Start of snippet'>anchor</a></sup>
Don't register these specific classes:
Usecase:
Imagine you have an IProcessor
(Interface), DatabaseProcessor
and APIProcessor
and UserProcessor
- In your appsettings.json you can register or unregister a specific implementations for this interface.
When your consumer takes an IEnumerable<IProcessor>
- you can register or unregister specific processors, and only run selected processors.
<a id='snippet-CodeExample-ConfigurationNotSpecificClass'></a>
var appsetting = """
{
"DependencyInjection": {
"Assembly:RonSijm.Syringe.ExamplesC": {
"Type:ExampleC2": "None",
"Type:ExampleC3": "None",
}
}
}
""".ToConfiguration();
<sup><a href='/src/Tests/RonSijm.Syringe.Tests/Features/Tests/MicrosoftServiceProvider/Registration/Config/ExamplesC_TypeRegistrationNone.cs#L11-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-CodeExample-ConfigurationNotSpecificClass' title='Start of snippet'>anchor</a></sup>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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 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. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.Configuration.Json (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- RonSijm.Syringe.Lib (>= 0.2.1)
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.2)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.2)
- Microsoft.Extensions.Configuration.Json (>= 9.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.2)
- RonSijm.Syringe.Lib (>= 0.2.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on RonSijm.Syringe:
Package | Downloads |
---|---|
RonSijm.Blazyload
A C# Blazor library to effortlessly implement Lazy Loading and Dependency Injection |
GitHub repositories
This package is not used by any popular GitHub repositories.