0

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?

Steven
  • 166,672
  • 24
  • 332
  • 435
Snehal
  • 329
  • 3
  • 14

2 Answers2

3

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

For<MembershipProvider>().Use(MockMembership.Object);
Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40
0

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
  • 2,150
  • 18
  • 28
  • 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 Oct 27 '10 at 22:47
  • 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 Oct 29 '10 at 15:11
  • i don't see For and Use with latest version of SM 2.5.3 – Snehal Dec 08 '10 at 17:01
  • The latest version is 2.6.1, get it here: https://github.com/downloads/structuremap/structuremap/StructureMap2.6.1.zip – Robin Clowers Dec 08 '10 at 17:14