开发者

Ninject to StructureMap

开发者 https://www.devze.com 2023-01-22 02:26 出处:网络
I am looking to con开发者_Python百科vert followingcode to StructureMap: private Mock<MembershipProvider> MockMembership = new Mock<MembershipProvider>();

I am looking to con开发者_Python百科vert 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?


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

For<MembershipProvider>().Use(MockMembership.Object);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消