/CSF.DecoratorBuilder

A small library providing an intuitive way to build services which make use of the decorator pattern

Primary LanguageC#MIT LicenseMIT

Builder for services using the decorator pattern

Build status Quality Gate status Test coverage

This library presents a fluent builder to configure how services are assembled using the decorator or chain of responsibility patterns. Full documentation is available on the project website.

Example

Here is a brief example of usage, for a project that uses Microsoft.Extensions.DependencyInjection.

using Microsoft.Extensions.DependencyInjection;

serviceCollection.AddDecoratorBuilder();

serviceCollection.AddTransient<ServiceImplementation>();
serviceCollection.AddTransient<ServiceDecoratorOne>();
serviceCollection.AddTransient<ServiceDecoratorTwo>();

serviceCollection.AddTransientDecorator<IServiceInterface>(d =>
    d.UsingInitialImpl<ServiceImplementation>()
     .ThenWrapWith<ServiceDecoratorOne>()
     .ThenWrapWith<ServiceDecoratorTwo>()
);

This example configures DI such that when a dependency upon IServiceInterface is resolved, the object received would be:

  • An instance of ServiceDecoratorTwo ...
  • ... wrapping an instance of ServiceDecoratorOne ...
  • ... wrapping an instance of ServiceImplementation

Supported environments

This library is built for netstandard2.0, offering a wide range of framework support. It offers packages which support either/both of:

  • Microsoft.Extensions.DependencyInjection
    • Includes any DI container which exposes an implementation of IServiceCollection
  • Autofac