In unity, when configuring a type, you can specify more than o开发者_JAVA百科ne typeConfig if uniquely named like so:
<type type="IFoo" mapTo="ConcreteFoo">
<typeConfig name="rainbows">
<constructor>
<param name="magic" parameterType="string">
<value value="rainbows"/>
</param>
</constructor>
</typeConfig>
<typeConfig name="unicorns>
<constructor>
<param name="magic" parameterType="string">
<value value="unicorns"/>
</param>
</constructor>
</typeConfig>
</type>
When resolving, how do you specify which typeConfig, and consequently what gets injected to the constructor?
Supply the name in a DependencyAttribute, like this:
[Dependency("unicorns")]
When you call container.Resolve, you can pass a string. That string is the name that you used to configure a particular registration.
Also, when you configure registrations, you can specify the dependencyName on a registration to tell it which named registration you want the container to use.
精彩评论