views:

48

answers:

2

I am looking to convert following code to StructureMap:

private Mock<MembershipProvider> MockMembership = new Mock<MembershipProvider>();

private StandardKernel GetIoCKernel()
{
    var modules = new IModule[]
    {
        new InlineModule(
            new Action<InlineModule>[]
            {
                m => m.Bind<MembershipProvider>()
                    .ToConstant(MockMembership.Object),
            })
    };

    return new StandardKernel(modules);
}

Mainly I am looking for the equivalent of the ToConstant method in StructureMap. Can anyone please help me?

+2  A: 

Assuming ToConstant() means "use this instance", the equivalent in StructureMap is:

For<MembershipProvider>().Use(MockMembership.Object);
Joshua Flanagan
A: 

Since ToConstant does not mean singleton, you want this:

private StandardKernel GetIoCKernel()
{
    return new Container(c => c.For<MembershipProvider>().Use(() => MockMembership.Object));
}

When you passing a delegate into For(), StructureMap will default to transient.

Robin Clowers
ToConstant (or any of its To* siblings) doesnt imply a scope (and the default is transient). Having said that, that distinction is pretty moot given it'll always be the same object
Ruben Bartelink
If you instruct the container "everytime I request an object that implements this interface/type, I want you to give me this specific instance", then I would call that a singleton.
Joshua Flanagan