这个ASP.net MVC系统设计有什么问题吗?

发布于 2024-10-15 23:03:41 字数 623 浏览 3 评论 0原文

我有一个 ASP.Net MVC 3 照片库,它是这样设计的:

Data Repositories(IImageRepoSitory, ITagRepository etc)
      |
   Services (IGalleryService, IWebService etc)
      |
  Web Application

我使用 Ninject 将所需的服务和存储库注入到 Web 应用程序中。

在使用实际数据库之前,我使用了一个简单的 ArrayList(和 JSON 序列化)作为我的持久逻辑(这将是 JsonImageRepository/JSonTagRepository),它工作得很好。但后来,我迁移到EF4 CTP5(Code First),出现了很多问题。基本上,我将这些存储库和服务作为 Singleton 注入(在 Global.asax.cs 中声明),但是当我有多个线程访问存储库时,它会说:

数据连接已关闭。

我在 Ninject 中更改为线程模式或请求模式,但引发了各种异常(关于上下文的多个实例,所以我认为 Singleton 应该是唯一的选择)。

设计有什么问题吗?或者我应该如何配置这些组件?

I have a ASP.Net MVC 3 photo gallery, which is designed in this way:

Data Repositories(IImageRepoSitory, ITagRepository etc)
      |
   Services (IGalleryService, IWebService etc)
      |
  Web Application

Which I use Ninject to inject the required Services and repositories into the web application.

Before I use actual database, I used a simple ArrayList (and JSON serialization) as my presistent logic (That will be JsonImageRepository/JSonTagRepository) which works perfectly fine. But later on, I moved to EF4 CTP5 (Code First), and many problems appeared. Basically, I injected those repositories and services as Singleton (which declared in Global.asax.cs), but when I have several threads that access the repositories, it saids:

Data Connection is closed.

I changed to something like Thread Mode or Request Mode in Ninject but various exception raised (regarding to multiple instances of context, so I think Singleton should be the only option).

Is there anything wrong with the design? or how should I configure those components?

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

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

发布评论

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

评论(1

执手闯天涯 2024-10-22 23:03:41

通常,存储库访问应该在请求范围内(至少是更改数据的范围)。我建议查看 bob 的有关使用 Ninject 和 NHibernate 实现存储库模式的博客文章。对于 EF4 应该几乎相同:

http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to- Fluent-nhibernate-and-mysql/

http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/

http://blog.bobcravens.com/2010/09/the-repository -pattern-part-2/

我计划在不久的将来将其添加到示例应用程序中。

Normally, repository access should be in request scope (at least the ones that change data). I recommend looking at bob's blog posts about a repository pattern implementation using Ninject and NHibernate. It should be pretty much the same for EF4:

http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to-fluent-nhibernate-and-mysql/

http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/

http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/

I planned adding this to the sample application in near future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文