.net – Structuremap – 多个接口实现
发布时间:2020-12-16 04:33:09 所属栏目:asp.Net 来源:网络整理
导读:我对Structuremap完全不熟悉,并且对如何连接具有多个实现的接口感到困惑. 让我们说我有Controller1和Controller2.我有Interface1,它由两个独立的类Class1ForInterface1和Class2ForInterface1实现.在Controller1中我希望注入Class1ForInterFace1,而在Controll
我对Structuremap完全不熟悉,并且对如何连接具有多个实现的接口感到困惑.
让我们说我有Controller1和Controller2.我有Interface1,它由两个独立的类Class1ForInterface1和Class2ForInterface1实现.在Controller1中我希望注入Class1ForInterFace1,而在Controller2中我想要注入Class2ForInterface1. 如何使用structuremap连接它?看来我只能有一个接口到具体类型的映射? 谢谢! 解决方法
可以使用几个类与structuremap实现相同的接口.
如果为映射命名,则可以稍后使用该名称对其进行检索. For<MyInterface>().Add<Class1ForMyInterface>().Named("Class1"); For<MyInterface>().Add<Class2ForMyInterface>().Named("Class2"); 如果您想要Class1ForMyInterface,那么您可以调用 container.GetInstance<MyInterface>("Class1"); 还有几种方法可以在容器中映射所有这些 For<IController>().Add<Controller1>().Ctor<MyInterface>().Is(i => i.GetInstance<MyInterface>("Class1")); 或者,如果保留注册类型时返回的smartinsatance,则可以在映射中使用它. var class1 = For<MyInterface>().Add<Class1ForMyInterface>().Named("Class1"); For<IController>().Add<Controller1>().Ctor<MyInterface>().Is(class1); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 需要帮助区分WebMatrix,ASP.NET网页和Raz
- asp.net – 如何在Visual Studio中生成Web服务的WSDL文件
- asp.net – 为什么设置AssociatedUpdatePanelId时不会触发U
- ASP.NET自定义404和500错误页面
- 使用ASP.NET MVC4 jquery/javascript捆绑
- asp.net-mvc – 使用ASP.NET MVC 3.0进行日期验证
- asp.net-mvc – 你如何指定在列表框中显示多少项目(高度)
- asp.net – 如何创建数据库并在安装过程中填充它
- ASP.NET MVC Web应用程序与ASP.NET Web应用程序
- asp.net-mvc – 当前上下文中不存在’HttpClientFactory’