使用 Entity Framework 4 和存储库模式的多层架构
我必须使用 Entity Framework 4.1 和 ASP.NET 构建 Web 应用程序的体系结构。我已经有了数据库结构,所以我必须使用database-fist。我在这里阅读了很多文章和主题,但似乎我遗漏了一些东西。我决定按以下方式组织项目:
我使用 Linq2Sql 构建了一个 Web 应用程序。我使用了这个项目。它提供了一个 T4 模板,为每个实体生成特定的静态存储库类。我喜欢这种方法,因为可以轻松地向任何存储库添加额外的逻辑,例如 GetUserByName()
。我喜欢这种方法,但到目前为止我找不到 EF4 的类似方法。我只找到通用存储库,然后我必须手动创建具体存储库。在这种情况下,我不喜欢的首先是我正在开发的应用程序有一点复杂的业务逻辑,所以我必须为几乎每个实体手动创建具体的存储库。其次,如果一开始我使用通用存储库来获取所有实体,后来我需要使用,例如GetUserByName()
,那么代码中就会出现不一致。我希望所有数据检索都以相同的方式完成。
我要么在架构结构中遗漏了一些东西。
Generic:如果需要分离,则为通用存储库
DAL:包含上下文类和存储库的 EDMX(实体模型)文件
>BLL:系统的业务逻辑
-- 实体
-- 服务
-- 等
UI:ASP.NET 页面
问题:
逻辑分离是否正确?
我应该使用特定的存储库吗?
为了获得最佳项目组织和易用性,您会推荐哪种存储库模式实现?
使用静态存储库更好吗?
谢谢
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:
Is the separation of logic correct?
Should I use specific repositories?
What implementation of repository pattern would you recommend for the best project organization and easiness to use?
Is it better to use static repository?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用静态类作为存储库。对于正确的面向对象设计来说这是可怕且遥远的。它还完全阻止了任何控制反转和依赖注入的可能性。
如果您想使用存储库模式,则必须使用特定的存储库。通用存储库只是 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.我的首选设置是:
My Preferred setup is: