z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
141 stars 10 forks source link

Add support of conditional decorators #93

Closed z4kn4fein closed 4 years ago

z4kn4fein commented 4 years ago

Currently, the container picks every decorator with the same service type during resolution. It would be great to add more control over the decorator selection like:

class A : IService { }
class B : IService { }
class ADecorator : IService {}
class ServiceDecorator : IService {}

container
    .Register<IService, A>("A")
    .Register<IService, B>("B")
    .RegisterDecorator<IService, ServiceDecorator>()
    .RegisterDecorator<IService, ADecorator>(config => config.When(typeInfo => typeInfo.Type == typeof(A)));

container.Resolve<IService>("A"); // new ADecorator(new ServiceDecorator(new A()));
container.Resolve<IService>("B"); // new ServiceDecorator(new B());
z4kn4fein commented 4 years ago

Released in v3.2.2