温莎城堡组件 生活方式建议

发布于 2025-01-04 21:13:12 字数 461 浏览 4 评论 0原文

我正在寻找有关如何最好地配置这种情况的一些建议。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆一缕阳光ご 2025-01-11 21:13:12

任何依赖于 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

三五鸿雁 2025-01-11 21:13:12
  • 会话 PerWebRequest
  • 存储库取决于使用 ISessionProvider (可以是单例),如果它们与会话一起注入,它们就会绑定到会话的生命周期,因此 PerWebRequest
  • 验证器:创建成本昂贵???好的,如果他们需要存储库或会话,则与存储库本身
  • 服务一样适用于此处:可能也可以根据网络请求
  • Session PerWebRequest
  • Repositories depending on using a ISessionProvider (could be singleton), if they are injected with the session they are bound to the lifetime of the session so PerWebRequest
  • Validators : expensive to create ???? ok if they need a repo or a session same applies here as the repos itself
  • services: can probably be also per web request
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文