结构图属性注入 [英] structuremap Property Injection
问题描述
如何使用结构图对类的属性进行依赖注入
How to do Dependency Injection on Property of a class Using Structure Map
public class ContactController : Controller
{
public IContactService Service { get; set; }
public ContactController()
: this(null,null)
{
}
[SetterProperty]
public MembershipProvider Provider { get; private set; }
}
在这里,当我创建ContactController的实例时,我希望将提供程序设置为 Mock< MembershipProvider>
时,请帮助我如何执行此操作?模拟是Moq框架类
Here when i Create instance of ContactController i want provider to be set to Mock<MembershipProvider>
please help me how to go about doing this? Mock is Moq Framework class
推荐答案
如果使用的是Mock,则很可能会编写测试代码.如果是这样,您可能不需要使用诸如StructureMap的依赖项注入工具.只需在测试设置代码中将Provider属性手动设置为MemberhpProvider即可.
If you are using a Mock, you are most likely writing test code. If thats the case, you likely don't need a dependency injection tool like StructureMap involved. Just set the Provider property manually to your MembershpProvider in your test setup code.
controller.Provider = Mock<MembershipProvider>
如果您真的想使用StructureMap配置setter注入,请参见以下答案:将属性注入到操作过滤器中
If you really want to configure setter injection using StructureMap, see this answer: Property Injection into an Action Filter
这篇关于结构图属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!