【问题标题】:Does model or controller perform CRUD in asp.net?模型或控制器是否在 asp.net 中执行 CRUD?
【发布时间】:2018-10-26 05:21:01
【问题描述】:

我对 ASP.NET 和 MVC 还很陌生,我对 CRUD 逻辑感到困惑。我从资源中找到了以下解释,但是,据我所知,CRUD 是在控制器中执行的,例如,它们是通过 Index、Create、DeleteConfirm、Edit 等操作来实现的。我误解了这个概念吗?

【问题讨论】:

  • 我认为,这里的 Model 是指域模型,而不是 MVC 中的“M”。

标签: asp.net asp.net-mvc model-view-controller


【解决方案1】:

根据上图,模型是“Doamin Model”,并不是Slava Utesinov所说的MVC中的“M”,而是DDD(Domain-Driven Design)的概念。

在 ASP.NET MVC 中,我们执行 CRUD 的传统方式是在控制器的操作中,您的理解是正确的。

在 DDD 概念(领域驱动设计)中,我们在领域模型中进行 CRUD。

无论我们使用 DDD 架构还是传统方式,都需要基于基本的 MVC 架构。

更多关于 DDD 的信息供您参考: Domain-Driven Design – What is it and how do you use it?

【讨论】:

    【解决方案2】:

    是的,您是对的,CRUD 操作可以在 Controller 中通过操作执行,而 Model 可以帮助实现这一点。模型只不过是一个具有属性的类。 例如:“Employee”是具有不同属性的类,如“FirstName、LastName、EmployeeID、Email、DateOfJoining 等”。现在,如果您必须对此执行 CRUD 操作,那么您必须使用此“Employee”模型类在不同操作下的 Controller 类中编写代码。

    只有模型不能执行 CRUD 操作。

    【讨论】:

      【解决方案3】:

      您可以随心所欲地进行操作,但您将打破“关注点分离”的概念。控制器应该只关心它将调用或执行哪个视图或哪个操作。模型应该只用于你的数据的结构,它通常类似于你的数据库属性的外观。简而言之,你的模型(类模型)应该有最少的思考。例如,您有一个名为 Person 的表,其列为 IDPerson、FirstName、LastName。您的模型应该与此类似:

      public class Person {
           public IdPerson {get;set;}
           public FirstName {get;set;}
           public LastName {get;set;}
      }
      

      假设您有一个视图可以显示一个人的详细信息,这可能是某物 像这样:

      public class PersonController : Controller
      
      public ActionResult GetPerson(int IdPerson){
           PersonBusinessLogic pbl = new PersonBusinessLogic();
           Person p = pbl.GetPersonFromDatabase(id); //To add more consistency, the data access is on a separate class for better maintenance and to emphasize "Separation of Concerns"
      
      ViewResult vr = new ViewResult()
           {
                  ViewName = this.View,//This is where you assign the page if you have other pages for this action
                  ViewData = new ViewDataDictionary(ViewData)
                  {
                      Model = p
                  }                             
            };
      
            return vr;
      }
      

      为了你的原油:

      [HttpPost]
      public ActionResult CreatePerson(Person p)
      {
              try
              {
                  if (ModelState.IsValid)
                  {              
              PersonBusinessLogic pbl = new PersonBusinessLogic();
              pbl.CreatePersonToDatabase(p);
                      return RedirectToAction("Index", "Home");
                  }
              }
              catch(Exception ex){
                  ModelState.AddModelError("",ex.Message);
              }
      
              return View(p);
      }
      
      [HttpPost]
      public ActionResult UpdatePerson(Person p)
      {
              try
              {
                  if (ModelState.IsValid)
                  {              
              PersonBusinessLogic pbl = new PersonBusinessLogic();
              pbl.UpdatePersonToDatabase(p);
                      return RedirectToAction("Index", "Home");
                  }
              }
              catch(Exception ex){
                  ModelState.AddModelError("",ex.Message);
              }
      
              return View(p);
      }
      
      [HttpPost]
      public ActionResult DeletePerson(Person p)
      {
              try
              {
                  if (ModelState.IsValid)
                  {              
              PersonBusinessLogic pbl = new PersonBusinessLogic();
              pbl.DeletePersonByIDFromDatabase(p.IdPerson);
                      return RedirectToAction("Index", "Home");
                  }
              }
              catch(Exception ex){
                  ModelState.AddModelError("",ex.Message);
              }
      
              return View(p);
      }
      

      为了给你一个更好的想法,找到一些关于如何将 MVC 作为一个概念得到极大应用的文章,那么你将非常欣赏学习过程。

      【讨论】:

        猜你喜欢
        • 2015-04-24
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-07
        • 2012-10-26
        相关资源
        最近更新 更多