c# - MVC设计模式,服务层的目的?

标签 c# asp.net-mvc entity-framework design-patterns repository

假设我有以下 repo 模式:

interface IGenericRepo<T> where T : class
{
     IEnumerable<T> GetAll();
     T GetById(object id);
     void Insert(T obj);
     void Update(T obj);
     void Delete(T obj);
     void Save();
}

interface ICustRepo : IGenericRepo<Cust>
{
     IEnumerable<Cust> GetBadCust();
     IEnumerable<Cust> GetGoodCust();
}

public class CustRepo : ICustRepo<Cust>
{
     //implement method here
}

然后在我的 Controller 中:

public class CustController
{
     private ICustRepo _custRepo;

     public CustController(ICustRepo custRepo)
     {
         _custRepo = custRepo;
     }

     public ActionResult Index()
     {
         var model = _custRepo.GetAll();
         return View(model);
     }

     public ActionResult BadCust()
     {
         var model = _custRepo.GetBadCust();
         return View(model); 
     }
}

基本上我的模式是这样的

View <-> Controller -> Repo -> EF -> SQL Server

但是我看到很多人这样做

View <-> Controller -> Service -> Repo -> EF -> SQL Server

所以我的问题是:

  1. 我为什么以及什么时候需要 service layer ?这不就是再增加一个不必要的层吗,因为每个非泛型方法都已经在 ICustRepo 中实现了。 ?

  2. 服务层是否应该返回DTO或者我的 ViewModel ?

  3. 服务层应该与我的存储库 1:1 映射吗?

我已经看了几天,但我对答案并不满意。

任何帮助将不胜感激,对于糟糕的英语表示歉意。

谢谢。

更新:

Difference between Repository and Service Layer?

我已经读过了。我已经知道这两者之间的区别,但我想知道原因和目的。所以这不能回答我的问题

最佳答案

长话短说

  1. 查看下面的解释
  2. 服务层之上的层不应“意识到”服务层之下存在更多层。
  3. 不一定,因为例如您可以将 1 种类型的数据分散在 2 个表中,而“核心”只看到一个,数据访问层负责“分组”并返回服务层类型

解释

典型的 3 层架构由表示层、服务/领域层、数据访问层 (DAL) 组成。

将服务层视为应用程序的“核心”。通常,服务层仅具有将在 DAL 中实现的存储库接口(interface)。

因此,它允许您“轻松”切换访问数据的方式。服务层返回的对象不应该是 DAO 的,因为毕竟表示层甚至“不知道”DAL 的存在。

场景: 您有一个 3 层解决方案。目前拥有所有层没有多大意义。

      /-------------------\
      |      Web App      | <--- Presentation Layer
      |-------------------|
      |  Service Library  | <--- Service Layer
      |-------------------|
      | Entity Framework  | <--- Data Access
      \-------------------/

现在你想在 ASP.NET MVC WebApi 中有一个 REST API

      /--------------------\
      | Web App | REST API | <--- Presentation Layer
      |--------------------|
      |  Service Library   | <--- Service Layer
      |--------------------|
      |  Entity Framework  | <--- Data Access
      \--------------------/

例如,现在您不再希望使用 Entity Framework 作为数据访问,而是希望使用 NHibernate。

      /--------------------\
      | Web App | REST API | <--- Presentation Layer
      |--------------------|
      |  Service Library   | <--- Service Layer
      |--------------------|
      |     NHibernate     | <--- Data Access
      \--------------------/

请注意,我们添加了一种新的表示形式并改变了我们访问数据的方式,但服务层从未改变。

通常,服务层公开要在数据访问层中实现的接口(interface),以便我们获得所需的“抽象”。

我在大学里用这种架构实现了一个项目。您可以查看代码 HERE

希望对您有所帮助。对不起,如果我这么无聊@解释事情:P

关于c# - MVC设计模式,服务层的目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180816/

相关文章:

c# - 获取所有匹配项的索引

c# - Entity Framework 存储过程 - 使用 CodeFirst 的多个结果集

c# - 自定义错误在本地工作,而不是在 IIS 上部署后

c# - 保存添加额外小数位

c# - 查询 ObjectContext 中的派生实体(每个层次结构一个表)

c# - 比特币哈希算法

c# - 使用 LINQ 从子列表中选择最大值

c# - 我应该调用 SaveChanges 一次还是在每次更改后调用?

c# - 设置数据库模式名称而不设置表名称

c# - .NET 图像是全黑的吗?