服务层和 ASP.NET MVC 2 的目的 [英] The Purpose of a Service Layer and ASP.NET MVC 2

查看:17
本文介绍了服务层和 ASP.NET MVC 2 的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了理解 MVC 2 并尝试让我的公司采用它作为未来开发的可行平台,我最近阅读了大量书籍.在过去的几年里,我一直专门使用 ASP.NET,我有一些事情要做.

In an effort to understand MVC 2 and attempt to get my company to adopt it as a viable platform for future development, I have been doing a lot of reading lately. Having worked with ASP.NET pretty exclusively for the past few years, I had some catching up to do.

目前,我了解存储库模式、模型、控制器、数据注释等.但有一件事使我无法完全理解以开始研究参考应用程序.

Currently, I understand the repository pattern, models, controllers, data annotations, etc. But there is one thing that is keeping me from completely understanding enough to start work on a reference application.

第一个是服务层模式.我已经阅读了许多关于 Stack Overflow 的博客文章和问题,但我仍然不完全理解这种模式的目的.我在 MVCCentral 上观看了高尔夫跟踪器应用程序上的整个视频系列,还查看了他发布的演示代码,在我看来,服务层只是存储库模式的另一个包装器,根本不执行任何工作.

The first is the Service Layer Pattern. I have read many blog posts and questions here on Stack Overflow, but I still don't completely understand the purpose of this pattern. I watched the entire video series at MVCCentral on the Golf Tracker Application and also looked at the demo code he posted and it looks to me like the service layer is just another wrapper around the repository pattern that doesn't perform any work at all.

我还阅读了这篇文章:http://www.asp.net/Learn/mvc/tutorial-38-cs.aspx 并且它似乎在某种程度上回答了我的问题,但是,如果您使用数据注释来执行验证,这似乎没有必要.

I also read this post: http://www.asp.net/Learn/mvc/tutorial-38-cs.aspx and it seemed to somewhat answer my question, however, if you are using data annotations to perform your validation, this seems unnecessary.

我寻找了演示、帖子等,但似乎找不到任何可以简单解释该模式并为我提供使用它的令人信服的证据的内容.

I have looked for demonstrations, posts, etc. but I can't seem to find anything that simply explains the pattern and gives me compelling evidence to use it.

谁能给我一个二年级(好吧,也许是五年级)使用这种模式的理由,如果我不这样做我会失去什么,如果我这样做我会得到什么?

Can someone please provide me with a 2nd grade (ok, maybe 5th grade) reason to use this pattern, what I would lose if I don't, and what I gain if I do?

推荐答案

在 MVC 模式中,您的职责在 3 个参与者之间分离:模型、视图和控制器.

In a MVC pattern you have responsibilities separated between the 3 players: Model, View and Controller.

模型负责做业务,视图呈现业务结果(也提供来自用户的业务输入),而控制器充当模型和视图之间的粘合剂,分离内部工作彼此之间.

The Model is responsible for doing the business stuff, the View presents the results of the business (providing also input to the business from the user) while the Controller acts like the glue between the Model and the View, separating the inner workings of each from the other.

模型通常由数据库备份,因此您有一些 DAO 可以访问它.您的企业做了一些……嗯……业务并在数据库中/从数据库中存储或检索数据.

The Model is usually backed up by a database so you have some DAOs accessing that. Your business does some...well... business and stores or retrieves data in/from the database.

但是谁来协调 DAO?控制器?不!模型应该.

But who coordinates the DAOs? The Controller? No! The Model should.

进入服务层.服务层将为控制器提供高级服务,并将在幕后管理其他(低级)玩家(DAO、其他服务等).它包含您应用的业务逻辑.

Enter the Service layer. The Service layer will provide high service to the controller and will manage other (lower level) players (DAOs, other services etc) behind the scenes. It contains the business logic of your app.

如果你不使用它会怎样?

您必须将业务逻辑放在某处,而受害者通常是控制器.

You will have to put the business logic somewhere and the victim is usually the controller.

如果控制器是以 Web 为中心的,它将必须接收其输入并以 HTTP 请求、响应的形式提供响应.但是,如果我想从与 RPC 或其他事物通信的 Windows 应用程序调用我的应用程序(并访问它提供的业务),该怎么办?然后怎样呢?

If the controller is web centric it will have to receive its input and provide response as HTTP requests, responses. But what if I want to call my app (and get access to the business it provides) from a Windows application which communicates with RPC or some other thing? What then?

好吧,您将不得不重写控制器并使逻辑客户端不可知.但是对于服务层,您已经拥有了.你不需要重写东西.

Well, you will have to rewrite the controller and make the logic client agnostic. But with the Service layer you already have that. Yyou don't need to rewrite things.

服务层提供与不绑定到特定控制器实现的 DTO 的通信.如果控制器(无论是什么类型的控制器)提供了适当的数据(无论来源如何),您的服务层就会为调用者提供服务,并将调用者从所涉及的业务逻辑的所有职责中隐藏起来.

The service layer provides communication with DTOs which are not tied to a specific controller implementation. If the controller (no matter what type of controller) provides the appropriate data (no mater the source) your service layer will do its thing providing a service to the caller and hiding the caller from all responsibilities of the business logic involved.

这篇关于服务层和 ASP.NET MVC 2 的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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