unity.mvc3 和 ntier 应用程序中的工作单元?

发布于 2024-12-19 04:09:53 字数 393 浏览 4 评论 0原文

慢慢地将MVC应用程序与类库放在一起:MVC>服务> I存储库> EF 存储库。目前,我的 EFRepository 实现都在其构造函数中创建一个新的 dbContext,但这会阻止测试,因此我需要注入 dbContext。

我在应用程序的其余部分成功地使用 unity.mvc3 进行 DI,但对如何正确实现 dbContext 注入感到困惑。

  • dbContext 是否充当工作单元?
  • 将 dbContext 注入存储库是否正确?
  • 我是否正确地认为 dbContext 需要根据请求实例化?

我想我只是有点迷失了从这里该去哪里。我认为我应该有一个工作单元,但在我看来这会发生在服务层。

感谢所有帮助,

詹姆斯

Slowly putting an MVC app together with class libraries: MVC > Services > IRepository > EFRepository. Now at the moment, my EFRepository implementations each create a new dbContext in their constructor but this prevents testing so I need to inject the dbContext instead.

I am successfully using unity.mvc3 for DI throughout the rest of my app but am confused as to how to correctly implement the dbContext injection.

  • Does a dbContext act as a Unit of Work?
  • Is it correct to inject a dbContext into a Repository?
  • Am I right in thinking that a dbContext needs to be instantiated per request?

Think I'm just a bit lost on where to go from here. I was thinking that I should have a Unit of Work but it seems to me this would happen at the service layer.

All help appreciated,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

山有枢 2024-12-26 04:09:53
  • 是(但您可以包装到自定义工作单元中)
  • 是(Unity.MVC3 中的分层生命周期管理器)
  • Yes (but you could wrap into a custom unit of work)
  • Yes
  • Yes (Hierarchical lifetime manager in Unity.MVC3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文