Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving different groups of collections
    primarykey
    data
    text
    <p>In the unit-test below, I'm trying to inject two different groups of a MyInterFace[] array, however it seem like I'm doing it the wrong way, since I'll get instances created of all my implementations of IMyInterface, which is rather inefficient....</p> <pre><code>namespace UnitTest.Interface { public interface IMyInterface { } } namespace UnitTest.ArrayGroup1 { public class MyInjected1 : IMyInterface { public static int Instancecount { get; set; } public MyInjected1() { Instancecount++; } } public class MyInjected2 : IMyInterface { public static int Instancecount { get; set; } public MyInjected2() { Instancecount++; } } } namespace UnitTest.ArrayGroup2 { public class MyInjected3 : IMyInterface { public static int Instancecount { get; set; } public MyInjected3() { Instancecount++; } } public class MyInjected4 : IMyInterface { public static int Instancecount { get; set; } public MyInjected4() { Instancecount++; } } } namespace UnitTest { public class MyClass1 { private IMyInterface[] collection; public MyClass1(IMyInterface[] group1collection) { collection = group1collection; } } public class MyClass2 { private IMyInterface[] collection; public MyClass2(IMyInterface[] group2collection) { collection = group2collection; } } public class MySubResolver : ISubDependencyResolver { private readonly IKernel kernel; public MySubResolver(IKernel kernel) { this.kernel = kernel; } public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) { return dependency.TargetType == typeof(IMyInterface[]); } public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) { IMyInterface[] res; if (dependency.DependencyKey == "group1collection") res= kernel.ResolveAll&lt;IMyInterface&gt;().Where(t =&gt; { var ns = t.GetType().Namespace; return ns != null &amp;&amp; ns.StartsWith("UnitTest.ArrayGroup1"); }).ToArray(); else res= kernel.ResolveAll&lt;IMyInterface&gt;().Where(t =&gt; { var ns = t.GetType().Namespace; return ns != null &amp;&amp; ns.StartsWith("UnitTest.ArrayGroup2"); }).ToArray(); return res; } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var kernel = new DefaultKernel(new DefaultProxyFactory()); kernel.Resolver.AddSubResolver(new MySubResolver(kernel)); var container = new WindsorContainer(kernel, new DefaultComponentInstaller()); container.Register( Classes.FromThisAssembly() .BasedOn&lt;IMyInterface&gt;() .WithServiceFirstInterface() .LifestyleTransient()); container.Register(Component.For&lt;MyClass1&gt;().LifeStyle.Transient); container.Register(Component.For&lt;MyClass2&gt;().LifeStyle.Transient); var sut=container.Resolve&lt;MyClass1&gt;(); Assert.AreEqual(MyInjected1.Instancecount, 1); Assert.AreEqual(MyInjected2.Instancecount, 1); Assert.AreEqual(MyInjected3.Instancecount, 0); Assert.AreEqual(MyInjected4.Instancecount, 0); } } } </code></pre> <p>How do I call ResolveAll in my SubResolver, so I only get instances created of the classes I really need??</p> <p>TIA</p> <p>/Søren</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. 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