views:

993

answers:

2

Given the below configuration

        Container.Register(Component.For<A>().Named("foo"));
        Container.Register(Component.For<B>().Named("foobar"));

        Container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("MyAssembly")
            .If(t => t.Name.EndsWith("ABC"))
            .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
            .WithService.Select(i => typeof(I))
        );

        Container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("MyAssembly")
            .If(t => t.Name.EndsWith("123"))
            .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
            .WithService.Select(i => typeof(I))
        );

If I know that the interface "I" exposes a property "P", and that the classes A and B can be assigned to P; how do I explicitly state that the first collection of types from the AllTypes call should have the property P set to the type with id of "foo", and the second collection should have the same property set to the type with the id of "foobar"?

Using XML config this can be done by explicitly setting the parameters using the ${id} notation. I assume its similar in the fluent API.

Thanks.

+1  A: 

You are on the right track - what you need to do is configure the parameters of each component, supplying the parameter named "P" with the value "${foo}" or "${foobar}" depending on your scenario, here's a working xunit fact (scroll down towards the bottom for the actual registration code) which demonstrates your scenario.

namespace Question651392
{
  public class First123 : I
  {
    public AbstractLetter P { get; set; }
  }

  public class Second123 : I
  {
    public AbstractLetter P { get; set; }
  }

  public class FirstABC : I
  {
    public AbstractLetter P { get; set; }
  }

  public class SecondABC : I
  {
    public AbstractLetter P { get; set; }
  }

  public interface I
  {
    AbstractLetter P { get; set; }
  }

  public abstract class AbstractLetter
  {
  }

  public class B : AbstractLetter
  {
  }

  public class A : AbstractLetter
  {
  }

  public class RegistrationFacts
  {
    [Fact]
    public void EnsureParametersCanBeSetWhenRegisteringComponentsInBulk()
    {
      WindsorContainer Container = new WindsorContainer();

      Container.Register(Component.For<A>().Named("foo"));
      Container.Register(Component.For<B>().Named("foobar"));

      Container.Register(
          AllTypes.Pick()
          .FromAssembly(GetType().Assembly)
          .If(t => t.Name.EndsWith("ABC"))
          .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
          .Configure(c=>c.Parameters(Parameter.ForKey("P").Eq("${foo}")))
          .WithService.Select(new[] { typeof(I) })          
      );

      Container.Register(
          AllTypes.Pick()
          .FromAssembly(GetType().Assembly)
          .If(t => t.Name.EndsWith("123"))
          .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
          .Configure(c => c.Parameters(Parameter.ForKey("P").Eq("${foobar}")))
          .WithService.Select(new[] { typeof(I)})
      );

      var all = Container.ResolveAll<I>();

      var firstABC = all.Single(i => i is FirstABC);
      Assert.IsType(typeof(A), firstABC.P);

      var first123 = all.Single(i => i is First123);
      Assert.IsType(typeof (B), first123.P);

      Assert.Equal(4, all.Count());
    }
  }
}

Hope this helps!

Bittercoder
+1  A: 

One thing to mention on testing this.

The second call to configure appears to cancel out the first call.

      .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
      .Configure(c => c.Parameters(Parameter.ForKey("P").Eq("${foobar}")))

If you add to the test

        var all2 = Container.ResolveAll<I>();
        Assert.IsTrue(all.Count(i => all2.Contains(i)) == 0);

it will fail, but you would expect it too pass as everything is declared transient. This implies the transient lifestyle was lost and the default lifestyle of singleton used instead.

Changing the configure call to the below results in a test pass.

        .Configure(c => .LifeStyle.Is(LifestyleType.Transient).Parameters(Parameter.ForKey("P").Eq("${foobar}")))

Thanks.

crowleym
You are right - I'm not sure if this ideal behaviour on the part of the fluent registration - being able to override a previous configuration is handy in some scenarios, but probably a little unexpected in most cases!
Bittercoder