views:

58

answers:

1

I'm using StructureMap 2.6.1
This is the code from Bootstrapper.cs:

ObjectFactory.Initialize(x => x.For<IFoo>().Use<Foo>());

When I run application, I get the following exception:

No Default Instance defined for PluginFamily IFoo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

I don't get an exception when I use this obsolete code:

ForRequestedType<IFoo>()
                .TheDefault.Is.OfConcreteType<Foo>();

Can anyone tell me the latest syntax for ObjectFactory's initializer?
Thank you.

+2  A: 

Each time you call Initialize, you're resetting the ObjectFactory. I.e. in the following scenario:

ObjectFactory.Initialize(x => x.For<IFoo>().Use<Foo>());
ObjectFactory.Initialize(x => x.For<IBaz>().Use<Baz>());

You've only actually mapped out IBaz to Baz.

You should use an ApplicationRegistry instead:

public class ApplicationRegistry : Registry
{
    public ApplicationRegistry()
    {
        For<IFoo>().Use<Foo>();
        For<IBaz>().Use<Baz>();
    }
}

And use that in your Initialize method:

ObjectFactory.Initialize(x => x.AddRegistry(new ApplicationRegistry()));
GenericTypeTea
I can map many items if I separate them by coma. The problem is that ObjectFactorz.Initiaze doesn't work in the ApplicationRegistry class. Can you tell me how to to pass argument to the constructor? I want to hard code it. The obsolete syntax was WithCtorArg("comeValue").EqualTo("someValue")
šljaker
Pass an argument to which constructor?
GenericTypeTea
Any class. For<Foo>().Use<Foo>().WithCtorArg("someValue").EqualTo("someValue");
šljaker
No idea, not done that yet. I suggest you ask a new question as that's totally different to the question asked here.
GenericTypeTea
It is: For<Foo>().Use<Foo>().Ctor<string>("someValue").Is("someValue");
šljaker