StructureMap:如何为每个接口使用多个具体类
我如何计算 StructureMap 为每个接口使用多个具体类。 即 IRepositoryCustomer 是通过以下方式实现的:RepositoryCustomerA 和 RepositoryCustomerB
如何向 StructureMap IoC 容器注册并解析它?
How can I figure StructureMap to use more than one concrete Class per Interface.
i.e. an IRepositoryCustomer is implemented by: RepositoryCustomerA and RepositoryCustomerB
How do I register and resolve this to/from the StructureMap IoC container?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您首先需要回答以下问题:“我什么时候需要 RepositoryCustomerA 而不是 RepositoryCustomerB?”
如果这些类中的任何一个都可以根据某些运行时状态在生产中实例化,那么您可能需要研究 条件对象构造。
如果 RepositoryCustomerA 应在开发时实例化(例如,用于单元测试),而 RepositoryCustomerB 是生产模拟,那么您需要两个不同的
Registry
:一个仅存在于测试代码中,另一个是在生产中可访问。有关此路线的更多信息,请阅读创建注册表作为配置 StructureMap 的一种方法。You first need to answer the question, "when do I want RepositoryCustomerA instead of RepositoryCustomerB?"
If either of those classes could be instantiated in production based on some runtime state, then you probably want to look into conditional object construction.
If RepositoryCustomerA should be instantiated at development time (for unit testing, say) while RepositoryCustomerB is the production analog, then you need two distinct
Registry
s: one that exists only in your test code, and one that is accessible in production. For more on this route, read about creating registries as a means of configuring StructureMap.