ZapInjector 1.0.2
dotnet add package ZapInjector --version 1.0.2
NuGet\Install-Package ZapInjector -Version 1.0.2
<PackageReference Include="ZapInjector" Version="1.0.2" />
<PackageVersion Include="ZapInjector" Version="1.0.2" />
<PackageReference Include="ZapInjector" />
paket add ZapInjector --version 1.0.2
#r "nuget: ZapInjector, 1.0.2"
#:package ZapInjector@1.0.2
#addin nuget:?package=ZapInjector&version=1.0.2
#tool nuget:?package=ZapInjector&version=1.0.2
ZapInjector
ZapInjector is a class library that helps to configure services into an IServiceCollection. It provides a way to load all the services defined into a set of assemblies basing on conventions or on explicit rules.
ZapInjector solves some problems that are commonly faced when using the default .NET Dependency Injection
- Scans assemblies and loads services basing on conventions without having to declare them in the Startup.cs
- Helps to set up complex services declarations through configurations rather than through code
- Helps to deal with configuration values and environment variables
Table of contents
- Installation
- Usage
- Strategies
- Load all services with convention strategy
- Load all services from explicit settings strategy
- Contributing
Installation
Install using the ZapInjector package
PM> Install-Package ZapInjector
Usage
When you install the package, it should be added to your csproj file. Alternatively, you can add it directly by adding:
<PackageReference Include="ZapInjector" Version="1.0.2" />
Define your configuration in the application.json or in the application.yaml files as done in this example In the Startup.cs file, in the ConfigureServices method, call the extension method LoadFromConfiguration
services.LoadFromConfiguration(Configuration);
Using Yaml
It is possible to convert the application.json file to a yaml file if you prefer. The example above uses NetEscapades.Configuration.Yaml as configuration provider for yaml files.
Strategies
The root elements of the ZapInjector's configuration are strategies. It is possible to define multiple strategies in the same configuration.
The example below shows how the root of the configuration should look like:
{
"ZapInjector": [
{
"Strategy": "ZapInjector.Strategies.FirstStrategyName, ZapInjector",
"Configuration": {
// Strategy configuration
}
},
{
"Strategy": "ZapInjector.Strategies.SecondStrategyName, ZapInjector",
"Configuration": {
// Strategy configuration
}
}
]
}
Load all services with convention strategy
The goal of Load all services with convention strategy is to load all the services of the given assemblies basing on a convention.
{
"ZapInjector": [
{
"Strategy": "ZapInjector.Strategies.LoadAllServicesFromAssembliesWithConventionStrategy, ZapInjector",
"Configuration": {
"Assemblies": [
"Assembly1",
"Assembly2"
],
"Convention": "ZapInjector.Strategies.Conventions.ConventionName, ZapInjector"
}
}
]
}
Default Convention
The LoadAllServicesFromAssembliesDefaultConvention will add a ServiceDescriptor to the ServiceCollection for every concrete non-generic implementation of a valid service type found in the exported types of the assemblies. A valid service type is either a non-generic interface or a non-generic concrete class. If multiple implementations are found for a given service type, they can be injected as IEnumerable<ServiceType>
Load all services from explicit settings strategy
The goal of Load all services with convention strategy is to provide developers a way to deal with complex services declarations through configuration files rather than through code.
{
"ZapInjector": [
{
"Strategy": "ZapInjector.Strategies.LoadServicesFromExplicitSettingsStrategy, ZapInjector",
"Configuration": {
"Values":[
{
// a valid value object. Check the Values section below
}
],
"ServiceDescriptions":[
{
// a valid service description object. Check the Service Descriptions section below.
}
]
}
}
]
}
Service Descriptions
The Service Description object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| ServiceName | string | false | The name to use to reference this service | MyServiceName | If this property is provided, the service can be referenced from a Reference object |
| ServiceType | string | true | The full name of the service type | ZapInjector.Examples.Abstractions.INameAbstraction, ZapInjector.Examples.Abstractions | |
| ImplementationType | string | false | The full name of the implementation type | ZapInjector.Examples.Implementations.NameImplementation, ZapInjector.Examples.Implementations | Only one of ImplementationType and ImplementationFactory can be provided |
| ImplementationFactory | Factory | false | The factory which creates the implementation instance | Only one of ImplementationType and ImplementationFactory can be provided | |
| Dependencies | Array of Parameter | false | The service dependencies that will be provided from the configuration instead of from the service provider | ||
| OnAfterCreate | Array of MethodCall | false | Methods to be called on the implementation instance after it's created | ||
| ServiceLifetime | string | false | The service lifetime. The allowed values are Singleton, Scoped and Transient. The default value is Scoped. |
Scoped |
Parameters
The Parameter object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| Reference | Reference | false | The Reference object | Only one of Reference, ServiceDescription and Value can be provided | |
| ServiceDescription | ServiceDescription | false | The ServiceDescription object | Only one of Reference, ServiceDescription and Value can be provided | |
| Value | Value | false | The Value object | Only one of Reference, ServiceDescription and Value can be provided |
Values
The Value object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| Name | string | false | The name to use to reference this value | MyValueName | If this property is provided, the value can be referenced from a Reference object |
| Type | string | true | The name of the type of the value | System.String | Only primitive types are supported |
| Value | string | true | The value | MY_STRING | It can be a string like MY_STRING or it can reference an environment variable using env(ENVIRONMENT_VARIABLE_NAME) |
References
The Reference object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| Name | string | true | The name of the referenced ServiceDescription or Value object | MyReferenceName |
Factories
The Factory object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| StaticReference | string | false | The full name of the type on which method will be called as a static method | ZapInjector.Examples.ImplementationsTwo.NameImplementationFour, ZapInjector.Examples.ImplementationsTwo | Use this property if the factory method is a static method. Only one of StaticReference and DynamicReference can be provided |
| DynamicReference | Parameter | false | The parameter referring to the object instance on which the method will be called as an instance method | Use this property if the factory method is an instance method. Only one of StaticReference and DynamicReference can be provided | |
| Method | Method Call | true | The factory method | ||
| InstanceType | string | false | The full name of the instance returned by the factory method | ZapInjector.Examples.Implementations.NameImplementation, ZapInjector.Examples.Implementations |
Method Calls
The Method Call object has the following properties
| Name | Type | Is Mandatory | Description | Example | Notes |
|---|---|---|---|---|---|
| MethodName | string | true | The name of the method to call | Create | |
| Parameters | Array of Parameter | false | The method arguments |
Contributing
The Contributing guide can be found here
Authors
<meta name="google-site-verification" content="tlDU5aubLJP35vfWNBN7r2mZttTe6-RDxWoNNK90dk4" /> <meta name="google-site-verification" content="kNyIwbDfMm4yRe9G8ERQIFn1V4jYhHzgHiF8DWXhn-Y" />
| 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. 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. |
-
net6.0
- FluentValidation (>= 11.0.1)
- Microsoft.Extensions.Configuration (>= 6.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
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 |
|---|