I have an interface:
public interface IFoo{
string Bar{ get; set; }
}
and an implementation of the interface
public class RealFoo{
public string Bar{ get; set; }
public string Qux{ get; set; }
}
I have configured unity, via the configuration file, to resolve IFoo to RealFoo, using property injection:
<register type="Namespace.IFoo, Assembly" mapTo="Namespace.RealFoo, Assembly">
<property name="Qux" value="somevalue" />
</register>
If I call Resolve(typeof(RealFoo)), my instance has .Qux set to “somevalue”. How is this possible? Is this expected behavior? I understand that if I call Resolve(typeof( IFoo )) that .Qux would be set to “somevalue”, but cannot explain how resolving the concrete type would set .Qux.
When using unity registration, and injection parameters/constructors you are defining those for the type it is mapped to. I don’t use configuration files in unity, but in code it allows you to do the following:
Meaning different concrete types can have different injection parameters/properties. In unity, unless the registrations are named, the last registered concrete injection parameters are used.
Consider the following:
If i then did either
unityContainer.Resolve<IFoo>()orunityContainer.Resolve<FooOne>()both would have “anothervalue” injected into “Qux”, as it resolves both to aFooOneinstance and the last registration injected “Qux” as “anothervalue”.