c# - MVC4+EntityFramework架构

标签 c# asp.net-mvc asp.net-mvc-4 architecture entity-framework-5

遵循此处建议的设计 MVC3 and Entity Framework ,我尝试使用 MVC4 + EntitiFramework5 为 DAL、BL 和 Web 创建不同的层。

引自@Davide Piras

1 - ProjectName.Interfaces (Class library, entities's interfaces);

2 - ProjectName.DAL (Class library, the only one allowed to even know the EF is used, the POCO entities implement the interfaces of project 1 using another file where you redeclare same objects using partial classes...);

3 - ProjectName.BL (Class library, Business logic, references the two projects above 1 and 2);

4 - ProjectName.Web (ASP.NET MVC application, Presentation Layer, references two projects 1 and 3 but NOT 2);

我对BL和DAL之间的联系有疑问。 DAL知道EF,BL不应该..但是如何实现呢?我的意思是,我在两个层上创建了代表我的实体的类(这对我来说似乎有点重复..即使在 BL 中我将添加验证和其他内容),但是我如何将数据库值公开给 BL ?

在我的默认 MVC4 解决方案中

 DbSet<Entity> entity

我可以查询(.Find等)..我想我需要将它们映射到我的BL中(IQueryable?IEnumerable?Isomething??)

完全困惑..任何帮助都是感激

最佳答案

当您谈到 DAL 和 EF 时,界限可能会变得有点模糊。在某些情况下,您could consider EF the DAL 。但我通常不会让BL直接访问EF并将其抽象到更高的级别,以便您可以轻松地将EF换成您的ORM如果需要的话。我用Repository Design Pattern进一步抽象EF。此模式的另一个优点是它使单元测试变得更容易,并且您可以使用依赖项注入(inject)。我还使用Unit of Work Design Pattern处理系统中的交易。存储库和工作单元是 DAL 的一部分还是只是 EF。这可能是有争议的,而且我知道自己不再关心如何定义 DAL。以下是我建议在 MVC 4 项目中使用的层。

MVC 4 Architecture

应用程序或域层是您的 BL 层。我倾向于合并Service Layer中使用的概念。在这一层中,因为我还没有看到将其分离有任何好处。但也可以选择在顶部添加这一层。

关于c# - MVC4+EntityFramework架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502116/

相关文章:

c# - 正则表达式:匹配表达式周围最接近的开闭花括号内的所有内容

C# : dynamic polymorphism with non polymorphic classes

c# - 实现经过身份验证的用户更新状态作为对系统状态更改的 react 的最佳方法。 (ASP.NET MVC 网站)

c# - 当 url 末尾有 DOT (.) 时,Asp.Net MVC 路由操作方法不会调用

c# - 使用 Entity Framework 、POCO 和 MySQL 的最佳教程

asp.net-mvc-4 - ASP.NET 4.5尚未在Web服务器上注册

c# - 一些静态类问题

c# - Linq-To-Sql 查询中的模糊调用

asp.net-mvc - 洋葱架构——服务层职责

c# - 添加 View - 无法检索元数据