温莎城堡组件 生活方式建议
我正在寻找有关如何最好地配置这种情况的一些建议。我的 ASP MVC 应用程序中有以下组件。
- 控制器 - 根据 MVC 的要求是瞬态的
- 服务 - 控制器调用这些来访问业务层
- 验证器 - 服务使用这些来验证组件。创建它们的成本很高,因此我需要它们成为单例
- 存储库 - 服务和验证器使用它们来访问数据
- ISession - 配置为 PerWebRequest 的 NHibernate 会话
我正在研究如何最好地配置服务和存储库。从表面上看,它们似乎可以配置相同。存储库唯一的依赖项(在构造函数中传递)是 ISession。仅服务依赖项(在构造函数中传递)是存储库。当服务需要验证器时,它会从服务定位器获取它。验证器在构造函数中获取存储库。我认为因为验证器是单例传递瞬态存储库是有问题的,因为验证器将保留具有已处置 ISession 的存储库。
任何帮助将不胜感激。
I'm looking for some advice on how to best configure this situation. I have the following components in an ASP MVC application.
- Controllers - Transient by requirement of MVC
- Services - Controllers call these to get to the business layer
- Validators - Services use these to validate components. These are expensive to create so I need them to be Singleton
- Repositories - Services and Validators use these to access data
- ISession - NHibernate Session configured as PerWebRequest
I'm looking at how best to configure Services and Repositories. On the surface it seems like they could both be configured the same. Repositories only dependency (passed in on the constructor) is ISession. Services only dependencies (passed in on the constructor) are Repositories. When a Service needs a Validator it gets it from the ServiceLocator. Validators get Repositories in the constructor. I think that because Validators are Singleton passing Transient Repositories is problematic because the Validator will hold onto a Repository that has a disposed ISession.
Any help here would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何依赖于 UnitOfWork(ORM、自定义实现...)的存储库都必须是暂时的或至少是相同的 UoW 生活方式。
如果您的验证器需要通过存储库对存储进行“只读”访问...您可以考虑拥有一个单例存储库,但要非常小心其实现
Any repository that rely on an UnitOfWork(ORM, custom implementaton...) has to be transient or at least same UoW lifestyle .
In case your Validator needs a "read-only" access to the storage via Repository... you may consider to have a Singleton Repository but be very carefull on its implementation