Ninject to StructureMap

时间:2010-10-27 20:04:31

标签: dependency-injection ninject structuremap doi

我希望将以下代码转换为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);
}

主要是我在寻找StructureMap中ToConstant方法的等价物。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:3)

假设ToConstant()表示“使用此实例”,StructureMap中的等效项为:

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

答案 1 :(得分:0)

由于ToConstant并不意味着单身,你想要这个:

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

当您将委托传递给For()时,StructureMap将默认为瞬态。