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.
精彩评论