使用 Entity Framework 4 和存储库模式的多层架构

发布于 2024-12-09 09:44:54 字数 945 浏览 2 评论 0原文

我必须使用 Entity Framework 4.1 和 ASP.NET 构建 Web 应用程序的体系结构。我已经有了数据库结构,所以我必须使用database-fist。我在这里阅读了很多文章和主题,但似乎我遗漏了一些东西。我决定按以下方式组织项目:

我使用 Linq2Sql 构建了一个 Web 应用程序。我使用了这个项目。它提供了一个 T4 模板,为每个实体生成特定的静态存储库类。我喜欢这种方法,因为可以轻松地向任何存储库添加额外的逻辑,例如 GetUserByName()。我喜欢这种方法,但到目前为止我找不到 EF4 的类似方法。我只找到通用存储库,然后我必须手动创建具体存储库。在这种情况下,我不喜欢的首先是我正在开发的应用程序有一点复杂的业务逻辑,所以我必须为几乎每个实体手动创建具体的存储库。其次,如果一开始我使用通用存储库来获取所有实体,后来我需要使用,例如GetUserByName(),那么代码中就会出现不一致。我希望所有数据检索都以相同的方式完成。

我要么在架构结构中遗漏了一些东西。

  • Generic:如果需要分离,则为通用存储库

  • DAL:包含上下文类和存储库的 EDMX(实体模型)文件

    >
  • BLL:系统的业务逻辑

    -- 实体

    -- 服务

    -- 等

  • UI:ASP.NET 页面

问题:

  1. 逻辑分离是否正确?

  2. 我应该使用特定的存储库吗?

  3. 为了获得最佳项目组织和易用性,您会推荐哪种存储库模式实现?

  4. 使用静态存储库更好吗?

谢谢

I have to build the architecture of a web application using Entity Framework 4.1 and ASP.NET. I already have the database structure, so I have to use the database-fist. I have read lots of articles and threads here, but it seems like I'm missing something. I have decided to organize the projects in the following way:

I have architecuted a web app with Linq2Sql. I used this project. It provides a T4 template that generates a specific static repository class for each entity. I like this approach as it is easy to add additional logic to any repository, such as GetUserByName(). I like this approach but I cannot find a similar approach for EF4 so far. I have found only generic repositories and then I have to manually create the concrete repositories. What I don't like in this case is first that I have the application that I'm working on has a bit complex business logic, so I will have to manually create the concrete repositories for almost each entity. Second, if at first I use the generic repository to get all entities, and later I need to use, for example GetUserByName(), then there will be inconsistency in the code. I would prefer all data retreiving to be done the same way.

I am either missing something in the architecte structure.

  • Generic: Generic Repository if it is neccessary to be seperated

  • DAL: The EDMX (Entity Model) file with the context class and repositories

  • BLL: The business logic of the system

    -- Entities

    -- Services

    -- etc

  • UI: ASP.NET pages

Questions:

  1. Is the separation of logic correct?

  2. Should I use specific repositories?

  3. What implementation of repository pattern would you recommend for the best project organization and easiness to use?

  4. Is it better to use static repository?

Thanks

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

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

发布评论

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

评论(2

飘逸的'云 2024-12-16 09:44:54

不要使用静态类作为存储库。对于正确的面向对象设计来说这是可怕且遥远的。它还完全阻止了任何控制反转和依赖注入的可能性。

如果您想使用存储库模式,则必须使用特定的存储库。通用存储库只是 EF 相关类的包装器(其中 ObjectSet / DbSet 已经是 EF 依赖存储库)。您还应该在聚合根之上构建存储库,而不是在每个实体之上。

Don't use static classes as repository. It is awful and far away for correct object oriented design. It also completely blocks any possibility of inversion of control and dependency injection.

If you want to use repository pattern you must use specific repository. Generic repository is just a wrapper around EF related classes (where ObjectSet / DbSet is already EF dependent repository). You should also build your repository on top of aggregate roots not on top of every entity.

挽清梦 2024-12-16 09:44:54

我的首选设置是:

  • Project.Presentation (UI)
  • Project.Application (向消费者/UI 公开 DTO 并向域层执行命令的应用程序层)
  • Project.Domain
    • 实体[具有领域逻辑](订单、客户……)
    • 域名服务(TransferService、CreditCardService...)
    • 存储库接口(IOrderRepository、ICustomerRepository ...)
  • Project.Repositories.EF(使用特定的数据访问)实现存储库接口(OrderRepository、CustomerRepository)的技术
  • Project.Infrastructure(交叉)

My Preferred setup is:

  • Project.Presentation (UI)
  • Project.Application (Application layer which expose DTOs to consumer/UI and execute commands to the domain layer)
  • Project.Domain
    • Entities [with domain logic] (Order, Customer, ...)
    • Domain Services (TransferService, CreditCardService...)
    • Repository Interfaces (IOrderRepository, ICustomerRepository, ...)
  • Project.Repositories.EF (Data access using specific technology implementing repository interfaces (OrderRepository, CustomerRepository)
  • Project.Infrastructure (Cross cutting)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文