结构图和立面
我正在考虑为 IoC 容器返回的应用程序的一部分定义外观,在我的例子中是 StructureMap。外观有一个不带参数的构造函数,用于设置默认实现,还有另一个构造函数,以便我可以定义外观管理的不同子系统对象的自定义实现。
然而,由于 StructureMap 试图创建一个外观实例,调用具有最多参数的构造函数,因此如果没有注册其他子系统的实现,它将失败。
我的问题是:如果无法使用主构造函数,是否有任何方法告诉 StructureMap 尝试使用以下构造函数?或者我必须告诉 StructureMap 选择哪个构造函数?
I'm thinking of defining a facade for part of an application which is returned by an IoC container, in my case StructureMap. The facade has a constructor with no arguments that sets a default implementations, and another one so I can define custom implementations of the different subsystem objects the facade manage.
However, since StructureMap is trying to make an instance of the facade calling the constructor with the most arguments, it fails if no implementation of the other subsystems is registered.
My question is: Is there any way of telling StructureMap to try use the following constructor in line if it cannot use the main one? Or must I tell StructureMap which constructor to select?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,如果最贪婪的对象没有成功,则不可能让 StructureMap 尝试解析实例并回退到备用对象。
我认为使用 StructureMap 连接外观的依赖关系并在那里指定默认值是一个好主意。也许可以使用 Null 对象实现作为可能缺失的子系统的默认实现。
To my knowledge it's not possible to have StructureMap trying to resolve an instance and to fall back to alternate ctors if not successful with the greediest.
I think that it would be a good idea to wire up the facade's dependendencies using StructureMap as well and specify defaults there. Perhaps it's possible to use a Null object implementation as default for the subsystems that might be missing.
您可以指定一个委托来创建您的类型,如下所示:
You can specify a delegate that will create your type as follows: