将依赖注入组件排除在主代码库之外
我有一个 MVC 应用程序,它使用 ninject 将服务依赖项注入控制器,并且运行良好。但是,我也有一些域对象在其构造函数中需要这些服务,并且我想使用 ninject 解决这些依赖项,但不想在域对象程序集中直接引用 ninject。我在这里阅读了很多问题和答案,但我仍然不清楚解决这个问题的最佳方法。例如,我有一个 ShoppingCart 域对象,需要将 IProductCatalogService 的实例传递给其构造函数。创建购物车实例的最佳模式是什么?我可以引用根内核并调用它,但这意味着在我的域程序集中引用 ninject。我应该将对内核的访问包装在工厂类中吗?
有任何想法或建议欢迎!
I have a MVC app that uses ninject to inject service dependencies into controllers and it works well. However I also have some domain objects that require these services in their constructors and I want to resolve these dependencies using ninject, but don't want to reference ninject directly in my domain objects assembly. I have read lots of questions and answers here but its still not clear to me the best way to go about this. For example I have a ShoppingCart domain object that needs an instance of a IProductCatalogService passed to its constructor. What is the best pattern to create an instance of a shopping cart? I could have a reference to the root kernel and call out to that, but that would mean having references to ninject throughout my domain assembly. Should I wrap access to the kernel in a factory class?
Any thoughts or suggestions welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在域对象中提供服务通常被认为是不好的做法。我认为你需要重新思考你想要实现的目标。为什么购物车需要使用产品目录服务?
从域的角度来看,我假设购物车将由许多“商品”组成,具有总计等属性,并且可能会传递给订购服务。您的控制器操作将通过添加项目、删除项目等来更新购物车域。
如果您确实需要考虑此选项,请使用 commonservicelocator。这将分离出您对 ninject 的(直接)依赖。
It is usually considered bad practice to have services in domain objects. I think you need to rethink exactly what you are attempting to achieve. Why does a ShoppingCart need to consume Product Catalog Services?
From a Domain perspective I would assume that a ShoppingCart would consist of many 'items', have properties like total etc and potentially would be passed to an ordering service. Your controller actions would update the Shopping Cart domain by adding items, removing items, etc, etc.
If you really need to consider this option, is to use commonservicelocator. This will separate out your (direct) dependency on ninject.