-
-
Notifications
You must be signed in to change notification settings - Fork 839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RegisterDecorator does not play nicely when decorating items registered with .As<IFoo>() or with .AsImplementedInterfacs() #529
Comments
The reason this happens is the same reason as #272. Individual component registrations are processed differently than registration source registrations. When you use When you resolve types, the component registrations always get processed first, before the registration sources. This happens because in nearly every case you actually want that to happen - registration sources are what support things like the In your particular case, what I'd say you want to do is get a little fancier with your if block. bool turnDecoratorOn = false;
var cb = new ContainerBuilder();
var mainRegistration = cb.RegisterType<Foo>().Named<IFoo>("Foo");
if(turnDecoratorOn)
{
cb.RegisterDecorator<IFoo>(instance => new FooDecorator(instance), "Foo")
.As<IFoo>();
}
else
{
mainRegistration.As<IFoo>();
} Notice what I'm doing is conditionally adding the Obviously that's not optimal, but if you follow the #272 chain, you'll see the real fix for this is pretty non-trivial. For now I'm going to close this - there's a workaround for this specific issue, and the root cause is actually #272. |
Now that #272 has been fixed, this issue should have gone away, right? |
@alexandrnikitin No, but it may be a starting point for the fix here. The |
We're pushing to enhance decorator syntax and features, which will hopefully resolve this issue. I've created a larger meta-issue for tracking that at #880. In the meantime, I'll close this specific issue and hopefully we can handle everything at once. |
Let’s say at container builder “build time” we wanted to use configuration to turn on or off a particular decorator.
Test structures:
Prior to using RegisterDecorator to accomplish this the code would look like this:
And if the config value had it turned off it would test successfully like this:
The reason for this is because using the .As() or .AsImplementedInterfaces() sets up a ‘default value’ for anything that asks for IFoo unless it is over-written by additional registrations that occur on the containerbuilder afterwards.
However, in attempts to make this work with RegisterDecorator my decorated component was not resolved and instead my default component was always resolved:
Basically RegisterDecorator is not over-writing the default registration for the un-named type ‘IFoo’ even though we are registering our decorator it with the .As() statement.
If we remove the .As from the default registration it works as expected when turnDecoratorOn is true
…however now it will not resolve when turnDecoratorOn is false, because IFoo by itself is never registered:
The text was updated successfully, but these errors were encountered: