StructureMap 接线 - 请进行健全性检查
我对 IOC 和 StructureMap 很陌生,有一个 n 级应用程序,正在研究如何设置接线(ForRequestedType ...),只是想与有更多经验的人确认这是最好的方法!
我不希望我的 UI 应用程序对象直接引用我的持久层,因此无法连接此 UI 项目中的所有内容。
现在,我通过在每个项目中定义一个注册表类来使其工作,该类根据需要连接项目中的类型。上面的层注册其类型,并且还调用下面的程序集并查找注册表,以便通过层次结构注册所有类型。
例如,我有 UI、服务、域和持久性库。在我的服务层中,注册表看起来像这样,
Scan(x =>
{
x.Assembly("MyPersistenceProject");
x.LookForRegistries();
});
ForRequestedType<IService>().TheDefault.Is.OfConcreteType<MyService>();
这是在这样的设置中执行此操作的推荐方法吗?在这种情况下,是否有更好的方法以及这些方法的优点/缺点是什么?
Im new to IOC and StructureMap and have an n-level application and am looking at how to setup the wirings (ForRequestedType ...) and just want to check with people with more experience that this is the best way of doing it!
I dont want my UI application object to reference my persistence layer directly so am not able to wire everything up in this UI project.
I now have it working by defining a Registry class in each project which wires up the types in the project as needed. The layer above registers its types and also calls the assembly below and looks for registries so that all types are registered throught the hierrachy.
E.g. I have UI, Service, Domain, and Persistence libraries. In my service layer the registry looks like
Scan(x =>
{
x.Assembly("MyPersistenceProject");
x.LookForRegistries();
});
ForRequestedType<IService>().TheDefault.Is.OfConcreteType<MyService>();
Is this a recommended way of doing this in a setup such as this? Are there better ways and what are the advantages / disadvantages of these approaches in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来不错。
如果您使用默认约定,例如为 IOrderService 接口提供默认实现 OrderSevice,则可以通过在 StructureMap 中使用约定来减少接线。
WithDefaultConventions
是注册表中使用默认约定的方法。您还可以指定自己的约定并使用方法With
将其注册到注册表中,请参阅 StructureMap 文档This sounds good.
If you use default conventions, like having a default implementation OrderSevice for the interface IOrderService you can reduce the wiring by using conventions in StructureMap. The
WithDefaultConventions
is a method in the Registry to use default conventions. You can also specify your own convention and register it in the registry using the methodWith
, see the StructureMap documentation