c# - Unity resolves last registration when class registered with multiple interfaces -
public interface { } public interface b { } public interface c : a, b { } public class foo : c { } iunitycontainer unity = new unitycontainer(); unity.registertype<a, foo>(new transientlifetimemanager(), new injectionfactory(container => { throw new exception(); })); unity.registertype<b, foo>(new transientlifetimemanager(), new injectionfactory(container => { throw new exception(); })); unity.resolve<a>(); //uses registertype<b, foo> injectionfactory
i expected unity use "a" injectionfactory.
unity maintains 1 build plan per build key second registration overwriting first registration (for foo).
when registering, first mapping created between , foo , second applies injection members target type (foo) based on build key (type: foo, name: null).
so, mapping between , foo separate injection members applied foo.
to illustrate more overwriting of registration, believe following functionally equivalent configuration:
unity.registertype<foo>(new transientlifetimemanager(), new injectionfactory(container => { throw new exception(); })); unity.registertype<foo>(new transientlifetimemanager(), new injectionfactory(container => { throw new exception(); })); unity.registertype<a, foo>();
in general, if need different injection members applied same concrete type need use named registrations.
Comments
Post a Comment