structuremap Property Injection

爱⌒轻易说出口 提交于 2019-12-12 01:34:25

问题


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; }
}

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


回答1:


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>

If you really want to configure setter injection using StructureMap, see this answer: Property Injection into an Action Filter



来源:https://stackoverflow.com/questions/3951111/structuremap-property-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!