Allow injecting decorators created by `Decorate` directly into other classes on actual type
Is your feature request related to a problem? Please describe. Currently Extenject provides special syntax for decorations:
Container.Bind<IInterface>().To<A>().AsSingle();
Container.Decorate<IInterface>().With<B>();
Classes that want an IInterface now get a B.
I now also have a class C that needs a B directly. B does not appear to be bound automatically based on the Decorate statement, so I try something like:
Container.Bind<B>().ToSelf().AsSingle();
... And then a B is injected, but it seems as if it is a different instance than the one used for decorating.
I also tried the following solution, suggested on Gitter:
Container.Bind<IInterface>().To<A>().AsSingle();
Container.Bind<B>().To<B>().AsSingle();
Container.Decorate<IInterface>().With<B>().FromResolve();
But this yields Expected collection to be empty but instead found '1'. It is possible the order of the bindings does not impact anything here anyway, though.
Using FromResolve on the binding for B itself also fails, because there is no binding yet that resolves directly to.
Using FromInstance is also impossible, since I have no instance yet (and resolving it in the installer would be bad practice).
The only workaround that worked so far is to simply not use Decorate and decorate manually, which is unfortunate:
Container.Bind<IInterface>().To<A>().AsSingle().WhenInjectedInto<B>();
Container.BindInterfacesAndSelfTo<B>().AsSingle();
Describe the solution you'd like
Some way to address or bind objects created by Decorate directly, or have a binding automatically or implicitly created when decorating happens.
Describe alternatives you've considered
The alternative is to drop the Decorate syntax and only use it for cases where addressing the decorations is not necessary, or allow multiple instances to be created in case it does not matter for your business logic.
Additional context Link to the discussion on Gitter.
What might work in your case is something like
Container.Bind<IInterface>().To<A>().AsSingle();
Container.Decorate<IInterface>().With<B>();
Container.Bind<B>().FromMethod(ctx => (ctx.Container.Resolve<IInterface>() as B));
However, I'm not sure how you would proceed if you added another decorator one on top, like
Container.Bind<IInterface>().To<A>().AsSingle();
Container.Decorate<IInterface>().With<B>();
Container.Decorate<IInterface>().With<C>();
Container.Bind<B>().FromMethod(ctx => (ctx.Container.Resolve<IInterface>() as B));
Then the last line should fail to resolve B (since it is actually a C) and I have no idea how to resolve B in this case.