Sparcpoint.Extensions.DependencyInjection 1.5.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Sparcpoint.Extensions.DependencyInjection --version 1.5.1
NuGet\Install-Package Sparcpoint.Extensions.DependencyInjection -Version 1.5.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="Sparcpoint.Extensions.DependencyInjection" Version="1.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sparcpoint.Extensions.DependencyInjection --version 1.5.1
#r "nuget: Sparcpoint.Extensions.DependencyInjection, 1.5.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.
// Install Sparcpoint.Extensions.DependencyInjection as a Cake Addin
#addin nuget:?package=Sparcpoint.Extensions.DependencyInjection&version=1.5.1

// Install Sparcpoint.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=Sparcpoint.Extensions.DependencyInjection&version=1.5.1

Sparcpoint.Extensions.DependencyInjection

Decoration and child service extensions for Microsoft.Extensions.DependencyInjection

Installation

Package Manager Console

Install-Package Sparcpoint.Extensions.DependencyInjection

.NET Core CLI

dotnet add package Sparcpoint.Extensions.DependencyInjection

Usage

This library adds three extension methods to IServiceCollection

  • Decorate - Allows usage of the decorator pattern with Microsoft DI.
  • WithChildServices - Allows an isolated scope of service injection for a parent service.
  • DecorateWithChildServices (Alias Decorate) - Allows usage of the decorator pattern (including child services) with Microsoft DI.

Examples

Decoration

Decoration allows for extending functionality of a class without changing the interface or the original implementation. Instead, you wrap instances of implementations that provide common functionality.

An example might be that you have a service that sends and receives from an endpoint called DefaultEndpointClient : IEndpointClient. You would like to introduce logging and performance measurements. However, you know if you add it directly to your implementation, you cannot use those again. Instead, you decorate IEndpointClient with a LoggedEndpointClient : IEndpointClient and a MeasurePerformanceClient : IEndpointClient, both of which take a IEndpointClient into its constructor. Now you have

LoggedEndpointClientMeasurePerformanceClientDefaultEndpointClient

chained together decorating the inner services.

See Decorator Pattern

/*
 * Basic Usage
 * The most common usage of decorators
 */
ServiceCollection services = new();

// Add the root service
services.AddSingleton<IService, ServiceImplementation>();

// Decorate all services currently registered
services.Decorate<IService, LoggingImplementation>();

// Build the provider
IServiceProvider provider = services.BuildServiceProvider();

// When we provide the service, we will receive ServiceDecorator first, 
// while the inner service will be the ServiceImplementation
// LoggingImplementation -> ServiceImplementation -> IService
var instance = provider.GetRequiredService<IService>();
/*
 * Open Generic Usage
 * We can also decorate open generics
 */
ServiceCollection services = new();

// Add the root service
services.AddSingleton<IEventProcessor<ServiceEvent>, DefaultEventProcessor<ServiceEvent>>();

// Decorate all services currently registered
services.Decorate(typeof(IEventProcessor<>), typeof(MeasurePerformanceProcessor<>));

// Build the provider
IServiceProvider provider = services.BuildServiceProvider();

// Like the basic use case, we have decorated the previously registered services
// MeasurePerformanceProcessor<ServiceEvent> -> DefaultEventProcessor<ServiceEvent> -> IEventProcessor<ServiceEvent>
var instance = provider.GetRequiredService<IEventProcessor<ServiceEvent>>();

Child Services

Child Services allow for certain instances of services to be built with an isolated set of registered services separated from the main provider. This allows for sub-services to be different as needed from implementation to implementation

/*
 * Basic Usage
 */

ServiceCollection services = new();

// This service is registered with the root provider.
services.AddSingleton<IMessageBus, InMemoryServiceBus>();
services.WithChildServices<DefaultAccountService>(child => 
{
  // All services in this container will be used to create the DefaultAccountService
  // Extra services are ignored
  services.AddSingleton<IMessageBus>(p => new AzureServiceBus("connectionString"));
});

var provider = services.BuildServiceProvider();

// This will return a DefaultAccountService with an AzureServiceBus implementation
// DefaultAccountService -> AzureServiceBus
var accountService = provider.GetRequiredService<DefaultAccountService>();

// This will return the "default" implementation of InMemoryServiceBus
var messageBus = provider.GetRequiredService<IMessageBus>();
/*
 * Setting the Parent's Lifetime
 * By default, a parent is setup as a Singleton. If you need
 * a different lifetime, it is specified as a parameter on
 * WithChildServices(...)
 */

 // DefaultAccountService will have a Transient lifetime
 // and will be created on every provide of this service
services.WithChildServices<DefaultAccountService>(child => 
{
  services.AddSingleton<IMessageBus>(p => new AzureServiceBus("connectionString"));
}, lifetime: ServiceLifetime.Transient);
/*
 * Decoration Example
 * Sometimes you may not want to simply replace the parent service
 * Instead, you may want to decorate it
 */

ServiceCollection services = new();

// This service is registered with the root provider.
services.AddSingleton<IMessageBus, InMemoryServiceBus>();
services.WithChildServices<DefaultAccountService>(child => 
{
  services.Decorate<IMessageBus, LoggedMessageBus>();
});

var provider = services.BuildServiceProvider();

// This will return a DefaultAccountService with a LoggedMessageBus implementation
// DefaultAccountService -> LoggedMessageBus -> InMemoryServiceBus
var accountService = provider.GetRequiredService<DefaultAccountService>();

// This will return the "default" implementation of InMemoryServiceBus
var messageBus = provider.GetRequiredService<IMessageBus>();

Decorate w/ Child Services

Like above, we can also decorate while injecting child services, as necessary.

Caveat: This can only be used with a closed implementation. Open-generics are NOT supported.

/*
 * Basic Usage
 */
ServiceCollection services = new();

// Add the root service
services.AddSingleton<IService, ServiceImplementation>();

// Decorate all services currently registered (w/ Child Services)
services.DecorateWithChildServices<IService, LoggingImplementation>(child => 
{
  child.AddSingleton<IOtherService, OtherServiceImplementation>();
});
/*
 * Alternate Syntax
 */
ServiceCollection services = new();

// Add the root service
services.AddSingleton<IService, ServiceImplementation>();

// Decorate all services currently registered (w/ Child Services)
services.Decorate<IService, LoggingImplementation>(child => 
{
  child.AddSingleton<IOtherService, OtherServiceImplementation>();
});
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sparcpoint.Extensions.DependencyInjection:

Package Downloads
Sparcpoint.Extensions.Objects

Modules used to manage objects, their hierarchy and storage

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.4 41 4/30/2024
1.6.3 55 4/27/2024
1.6.2 99 4/22/2024
1.6.1 94 4/22/2024
1.6.0 91 4/21/2024
1.5.3 137 4/19/2024
1.5.2 87 4/18/2024
1.5.1 93 4/17/2024
1.4.0 106 4/3/2024
1.3.0 118 3/30/2024
1.2.0 83 3/27/2024
1.1.1 68 3/25/2024
1.1.0 85 3/25/2024
1.0.3 99 3/8/2024
1.0.1 112 2/13/2024
1.0.0 91 2/13/2024

Initial Offering