Skip to content

A simple and lightweight way of applying the Decorator Pattern using Asp Net's dependency injection services.

License

Notifications You must be signed in to change notification settings

SuricateCan/DependencyInjection.Decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DependencyInjection.Decorators

A simple and lightweight way of applying the Decorator Pattern using Asp Net's dependency injection services. This lib is built on top of Microsoft.Extensions.DependencyInjection.Abstractions and allows services to be added to the service provider using a decorator pattern.

Installation

To install this lib, simply install the package from NuGet using the command below.

Install-Package DependencyInjection.Decorators

How To Use

To register services in a decorator pattern you'll use one of the several extension methods for IServiceCollection.

Each service lifetime supported in ServiceLifetime enum has its own method:

  • For Scoped: AddDecoratedScoped;
  • For Transient: AddDecoratedTransient;
  • For Singleton: AddDecoratedSingleton;

Alternatively you can use AddDecorated and pass in the chosen service lifetime.

You'll find both generic and concrete extension methods.

Generic methods will support up to one component and five decorators. For more decorators use the concrete methods.

Examples

// generic methods
services.AddDecoratedScoped<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();
services.AddDecoratedTransient<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();
services.AddDecoratedSingleton<IServiceToDecorate, Decorator3, Decorator2, Decorator1, Component>();

//concrete methods
services.AddDecoratedScoped(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);
services.AddDecoratedTransient(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);
services.AddDecoratedSingleton(serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);

//one method to run them all
services.AddDecorated(serviceLifetime, serviceToDecorateType, decorator3Type, decorator2Type, decorator1Type, componentType);

How Does It Work

The lib uses the factory overload of IServiceCollection.Add method to register the service Type and do its job.

All types passed to AddDecorated methods, except the service type, are registered using the same service lifetime, so there is no need to register them prior to calling AddDecorated.

The factory for the service type, at runtime, instances all services registered, from right to left, using a combination of IServiceProvider and ActivatorUtilities, passing the last instanced service as constructor parameter for the next. This means that the call order, based on the example above, is: Decorator3 -> Decorator2 -> Decorator1 -> Component. (for more details on this matter, check the test project)

Rules Of Thumb

  1. All types are registered by the AddDecorated methods;
  2. All types are registered with the same lifetime;
  3. Instances are created from right to left, each one being inject into the next;
  4. At runtime, calls to the service execute from left to right.

Want to Contribute

If you find bugs and don't have time to submit a PR, please report it using Github isses. On the other hand, if you can submit a PR I'll gladly take a look at it.

About

A simple and lightweight way of applying the Decorator Pattern using Asp Net's dependency injection services.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages