【问题标题】:How we can create 3 tier architecture in ASP.Net MVC application with EF?我们如何使用 EF 在 ASP.Net MVC 应用程序中创建 3 层架构?
【发布时间】:2015-04-10 11:17:35
【问题描述】:

我正在使用实体框架 6 在 ASP.NET MVC 5 中创建一个新项目(Web 应用程序)。我曾使用 ASP.Net Web 表单应用程序在 3 层架构中工作,但我对处理实体框架、模型等感到困惑. 那么,如何使用 EF 在 MVC 中构建 3 层架构并且与实体框架一起使用是可行的?

【问题讨论】:

  • MVC 自然由 3 层组成,即模型、视图和控制器,EF 是位于 DB 和 C# 代码之间的 ORM,提供数据操作或数据访问转换,与它无关架构。模型是 MVC 弧中的一层,但 EF 与它无关。

标签: c# .net asp.net-mvc entity-framework-6 3-tier


【解决方案1】:

是的,您可以实现 3/N 层架构(或类似的架构)。

ASP.NET MVC 与实体框架有很好的合作。 EF 甚至在默认的 ASP.NET MVC 模板中安装并用于用户/角色管理(身份)。

典型的 ASP.NET MVC 应用程序由模型、视图和控制器组成。简要说明:

  • 模型是 POCO。模型封装数据并负责数据有效性。打个比方——这是应用层的一部分,也是数据层的一部分。数据层还包括 EF db 上下文类。
  • 视图是 .cshtml 文件,可以生成 html 并且可以进行强类型化(具有模型)。类比 - 这是表示层。
  • 控制器负责处理 HTTP 请求、检索数据模型并将模型传递给视图。打个比方 - 这是应用层(业务逻辑)的一部分。

通常,控制器将接收一些 viewModel,对其进行验证、处理并返回一些操作结果(视图、部分视图、JSON、文件等)。在流程部分,控制器可以初始化实体框架上下文,并通过EF db上下文等获取或保存数据到数据库。

“使控制器尽可能精简”几乎总是一个好主意,因此许多 ASP.NET MVC 解决方案使用存储库/工作单元或服务模式。

使用服务创建某些实体的 MVC 应用程序的一些典型部分的示例:

服务

// Connect to database through entity framework db context.
public interface IMyService
{
    MyDbContext DbContext { get; set; }

    IQueryable<Something> GetSomething( ... query params ... );
    void CreateSomething(Something model);
    // etc. 
}

控制器

public MyController
{
    private IMyService myService;

    public MyController(IMyService myService)
    {
        this.myService = myService;
    }

    // Showing create/edit form
    [HttpGet]
    public ActionResult CreateSomething()
    {
        // Create Empty view model. new SomeViewModel(); for example.
        // Get some nomenclatures for dropdowns etc. through the service (if needed).
        // return View(viewModel);
    }

    // Processing the post request
    [HttpPost]
    public ActionResult CreateSomething(SomeViewModel viewModel)
    {
        // Check validity of viewModel (ModelState.IsValid)
        // If not valid return View(viewModel) for showing validation errors
        // If valid map the viewModel to Model (through AutoMapper or custom mapping)
        // Call myService CreateSomething method.
        // Redirect to page with details.
    }
}

型号

public class Something
{
    public int Id { get; set; }
    public string Name { get; set; }
    // .. more properties (can be decorated with validation attributes)
}

演示视图模型

// View model for Something class. Including only properties related to the current UI.

public class SomeViewModel
{
    public int Id { get; set; }
    // .. more properties (can be decorated with validation attributes)
}

查看

@model SomeViewModel

<!-- Form -->

@Html.ValidationForModel()
@Html.EditorForModel()
submit button

<!-- /Form -->

【讨论】:

  • 和你的文章一样,我不需要创建 3 层架构,MVC 和 EF 的结合本身就是一个 3/N 层架构。对吧?
  • MVC 和 EF 的结合本身就变成了与 3 层架构非常相似但又不完全相同的东西(在 3 层架构的经典含义中)。我会推荐使用所描述的架构,但我不知道您是否需要这个或 3 层架构在该术语的经典含义中。
【解决方案2】:

是的,您可以实现 3 层架构师:

  1. Tier(Presentation):Views(这是 MVC 中 V 代表的意思
  2. Tier(逻辑):通常是 Models 和一些 Helperclasses(这是 M 的意思)
  3. 层(数据):借助实体框架,您可以从模型创建数据库。

Here's 一个关于如何将实体框架与 ASP.NET MVC 一起使用的教程。

【讨论】:

    【解决方案3】:

    以下是实施 3 层的方法:

    表示层包括 (MVC)
    业务逻辑层将包括(C# 编程 - 一个 dll)
    数据访问层将包括(使用实体框架或 dll 进行 C# 编程)
    业务对象层将包括(实体框架模型)

    参考:http://www.codeproject.com/Articles/841324/ASP-NET-MVC-Web-App-on-Tier-for-Beginners

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 1970-01-01
      • 2011-12-21
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 2010-11-11
      • 1970-01-01
      相关资源
      最近更新 更多