views:

291

answers:

1

I keep getting a stackoverflow exception when I call "GetInstance" (the last line). All, yes ALL of my types implement ITracker. MultiTracker has a constructor with a single parameter, which is an array of ITracker's.

It seems like StructureMap is ignoring the fact that I told it that MultiTracker is the default class I want when requesting the type ITracker.

I just can't get it to work. Any thoughts?

Container = new Container(x =>
 {
  //Multitracker takes ITracker[] in its constructor
  x.ForRequestedType<MultiTracker>().TheDefault.Is.OfConcreteType<MultiTracker>().TheArrayOf<ITracker>().Contains(z =>
   {
    z.OfConcreteType<ConcreteType1>(); //ConcreteType1 : ITracker
    z.OfConcreteType<ConcreteType2>(); //ConcreteType2 : ITracker
   });

  x.ForRequestedType<ITracker>().TheDefault.Is.OfConcreteType<MultiTracker>();
    });

//Run a test - this explodes
Container.GetInstance<ITracker>();
A: 

I'm not sure if I would do it this way but it works.....

    public void Initialize()
    {
        var container = new Container(x => 
            x.ForRequestedType<ITracker>()
            .TheDefaultIsConcreteType<MultiTracker>()
            .OnCreation(y => 
                ((MultiTracker)y).Trackers = new ITracker[]
                {
                    new ConcreteType1(), new ConcreteType2()
                }
            ));
        container.GetInstance<ITracker>();
    }

    public class MultiTracker : ITracker
    {
        public ITracker[] Trackers { get; set; }
    }
Bill