RegisterInstance and RegisterType side by side..

Dec 5, 2009 at 12:57 PM


the Unity documentation states, that RegisterType and RegisterInstance should not be used both for the same type, in order to avoid unexpected behaviour. However, how about this situation:

container.RegisterType<MyInjectedInterface, MyInjectedClass>();
MyInjectedInterface i = container.Resolve<MyInjectedInterface>();

// do something with i
// ...
// then register its modified instance
container.RegisterInstance<MyInjectedInterface>("injection", i);

When injected into a dependent class accepting a MyInjectedInterface as constructor parameter, and the following configuration:

  <param name="parameter" parameterType="MyInjectedInterface">
    <dependency name="injection" />

Will this resolve correctly? (Note the additional name..)

Best wishes,


Dec 5, 2009 at 6:43 PM

Yes, at first look what you've got there should work as you expect.