Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This behavior is not included in Unity but its not hard to retrofit.</p> <pre class="lang-cs prettyprint-override"><code>var container = new UnityContainer(); container.AddNewExtension&lt;MapParameterNamesToRegistrationNamesExtension&gt;(); container.RegisterType&lt;ICommand, LoadCommand&gt;("loadCommand"); container.RegisterType&lt;ICommand, SaveCommand&gt;("saveCommand"); container.RegisterType&lt;ViewModel&gt;(new MapParameterNameToRegistrationName()); var vm = container.Resolve&lt;ViewModel&gt;(); Assert.IsType(typeof(LoadCommand), vm.LoadCommand); Assert.IsType(typeof(SaveCommand), vm.SaveCommand); public class MapParameterNamesToRegistrationNamesExtension : UnityContainerExtension { protected override void Initialize() { var strategy = new MapParameterNamesToRegistrationNamesStrategy(); this.Context.Strategies.Add(strategy, UnityBuildStage.PreCreation); } } public class MapParameterNamesToRegistrationNamesStrategy : BuilderStrategy { public override void PreBuildUp(IBuilderContext context) { if (context.Policies.Get&lt;IMapParameterNameToRegistrationNamePolicy&gt;(context.BuildKey) == null) { return; } IPolicyList resolverPolicyDestination; IConstructorSelectorPolicy selector = context.Policies.Get&lt;IConstructorSelectorPolicy&gt;(context.BuildKey, out resolverPolicyDestination); var selectedConstructor = selector.SelectConstructor(context, resolverPolicyDestination); if (selectedConstructor == null) { return; } var parameters = selectedConstructor.Constructor.GetParameters(); var parameterKeys = selectedConstructor.GetParameterKeys(); for (int i = 0; i &lt; parameters.Length; i++) { Type parameterType = parameters[i].ParameterType; if (parameterType.IsAbstract || parameterType.IsInterface) { IDependencyResolverPolicy resolverPolicy = new NamedTypeDependencyResolverPolicy(parameterType, parameters[i].Name); context.Policies.Set&lt;IDependencyResolverPolicy&gt;(resolverPolicy, parameterKeys[i]); } } resolverPolicyDestination.Set&lt;IConstructorSelectorPolicy&gt;(new SelectedConstructorCache(selectedConstructor), context.BuildKey); } } public class MapParameterNameToRegistrationName : InjectionMember { public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { policies.Set&lt;IMapParameterNameToRegistrationNamePolicy&gt;(new MapParameterNameToRegistrationNamePolicy(), new NamedTypeBuildKey(implementationType, name)); } } public interface IMapParameterNameToRegistrationNamePolicy : IBuilderPolicy { } public class MapParameterNameToRegistrationNamePolicy : IMapParameterNameToRegistrationNamePolicy { } </code></pre> <p>The code and test can be found in the source code of the <a href="http://tecx.codeplex.com" rel="nofollow">TecX project on CodePlex</a>. Project <em>TecX.Unity</em> (folder <em>Injection</em>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload