在asp.net mvc中创建服务层

时间:2021-07-31 16:34:54

To update an Entity of database (table) we directly inherit Model Entity to view page like

要更新数据库实体(表),我们直接继承模型实体以查看页面

<%@ Page Title="Edit" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCProject.Models.Preson>" %>

And Then Post method of Edit is called from controller, and at last, Entity updates By savechanges method of LINQ class or EF.

然后从控制器调用Edit的Post方法,最后,实体更新LINQ类或EF的savechanges方法。

But user should allowed to update "Preson" Entity only for selected fields, rest of the fields should set automatically.

但是用户应该只允许为选定的字段更新“Preson”实体,其他字段应该自动设置。

User should have only selected values, to be editable in his view. And the Entity should be inherited from our service layer e.g. <MVCProject.ServiceLayer.Preson>.

用户应该只有选定的值,可以在他的视图中进行编辑。并且实体应该从我们的服务层继承,例如

This would be the best way to split application into tires.

这将是将应用分成轮胎的最佳方式。

Now, Anybody knows, How to create classes in service layer, and map them to databasecontext (in case of LINQ class)?

现在,任何人都知道,如何在服务层创建类,并将它们映射到databasecontext(在LINQ类的情况下)?

2 个解决方案

#1


To update selected values,

要更新选定的值,

  1. Use a viewmodel approach
  2. 使用viewmodel方法

  3. Within the controller allow only certain values to be binded (Include or Exclude)
  4. 在控制器内只允许绑定某些值(包含或排除)

#2


Examine this tutorial, it has heaps of info about abstracting away a Service layer for an MVC application.

检查本教程,它有大量关于为MVC应用程序抽象出Service层的信息。

#1


To update selected values,

要更新选定的值,

  1. Use a viewmodel approach
  2. 使用viewmodel方法

  3. Within the controller allow only certain values to be binded (Include or Exclude)
  4. 在控制器内只允许绑定某些值(包含或排除)

#2


Examine this tutorial, it has heaps of info about abstracting away a Service layer for an MVC application.

检查本教程,它有大量关于为MVC应用程序抽象出Service层的信息。