MVC设计模式,服务层用途? [英] MVC design pattern, service layer purpose?

查看:105
本文介绍了MVC设计模式,服务层用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下回购模式:

Let's say I have a following repo pattern :

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
}

然后在我的控制器中:

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); 
     }
}

基本上我的模式是类似的

Basically my pattern is something like

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

但是我看到很多人这样做

but I saw a lot of people doing this

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

所以我的问题是:

  1. 为什么以及何时需要service layer?

服务层应该返回DTO还是我的ViewModel?

Should the service layer return DTO or my ViewModel?

服务层是否应与我的存储库以1:1比例映射?

Should the service layer map 1:1 with my repo?

我已经环顾了几天,但我对答案不满意.

I've look around for few days but I haven't satisfied with the answers.

对于英语不好的帮助,我们将不胜感激.

Any help will be appreciated and apologize for bad english.

谢谢.

更新:

存储库与服务层之间的区别?

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

I've already read this. I already know the difference between those 2, but I wanna know why and the purpose. So that doesn't answer my question

推荐答案

TL; DR

  1. 请参阅下面的说明
  2. 服务层之上的层不应该意识到".服务层下存在更多层.
  3. 不是必须的,因为例如您可以将来自1个类型的数据分散在2个表中,并将核心"数据只能看到一个,数据访问层负责分组".并返回服务层类型
  1. See explanation below
  2. Layers above Service Layer should not be "aware" that more Layers exist below the Service Layer.
  3. Not necessarily, because you can have for example Data from 1 Type scattered across 2 tables and the "Core" only see's one, the Data Access Layer is responsible for "Grouping" and returning the Service Layer Type

说明

典型的3层体系结构由表示层,服务/域层,数据访问层(DAL)组成.

The typical 3-layer architecture is composed of Presentation Layer, Service/Domain Layer, Data Access Layer (DAL).

将服务层视为核心"层.您的应用程序.通常,服务层仅具有将在DAL中实现的存储库接口.

Think of the Service layer as the "Core" of your Application. Typically, the Service Layer only has Repository Interfaces that will be implemented in the DAL.

因此,它使您可以轻松"地进行操作.切换访问数据的方式.服务层返回的对象不应是DAO的对象,因为毕竟,表示层甚至不知道" DAO. DAL存在.

Therefore it allows you to "easily" switch the way you access data. The objects returned by the service layer should not be DAO's, because after all, the Presentation Layer doesn't even "know" the DAL exists.

场景: 您有3层解决方案.目前,拥有所有图层并没有多大意义.

Scenario: You have a 3-tiered Solution. Currently doesn't make much sense in having all layers.

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

现在您想在ASP.NET MVC WebApi中拥有REST API

Now you want to have a REST API in ASP.NET MVC WebApi

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

例如,现在您不再希望将Entity Framework用作数据访问权限,而希望使用NHibernate.

Now, for example, you no longer want to use Entity Framework as your Data Access and want to use NHibernate.

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

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

Notice that we added a new form of Presentation and switched the way we access Data, but the Service Layer never changed.

通常,服务层公开要在数据访问层中实现的接口,因此我们得到了抽象"信息.我们想要的.

Typically, the Service Layer exposes Interfaces to be implemented in the Data Access Layer so we get the "abstraction" we want.

我在大学实施了一个具有这种架构的项目.您可以查看代码此处

I implemented a project with this architecture in university. You can check out the code HERE

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

I hope this helped. Sorry if I'm so boring @ explaining things :P

这篇关于MVC设计模式,服务层用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆