RegisterInstance and RegisterType side by side..

Dec 5, 2009 at 11:57 AM

Hi,

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:

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

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

Best wishes,

Karsten

Dec 5, 2009 at 5:43 PM

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