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

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

问题描述

在努力了解MVC 2,并试图让我的公司采用它作为未来发展的一个可行的平台,我一直在做大量的阅读最近。经过与ASP.NET pretty专门为工作在过去的几年中,我有一些赶上做的。

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.

首先是服务层图案。我看了很多博客文章和问题在这里对堆栈溢出,但我还是不完全理解这个模式的目的。我看了整个系列影片在MVCCentral的高尔夫跟踪应用程序,也看了演示code他张贴,它看起来对我来说,服务层就在存储库模式的另一个包装,根本不进行任何工作所有

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.

我也看了这个帖子:<一href=\"http://www.asp.net/Learn/mvc/tutorial-38-cs.aspx\">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.

可有人请我提供一个二年级(好吧,也许5年级),之所以用这种模式,如果我不我会失去,如果我做什么,我得到什么?

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?

推荐答案

在你有3名球员之间的分离责任MVC模式:模型,视图和控制器

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

示范负责做商业的东西,查看presents业务的结果(从用户还提供了输入业务),而控制器的行为就像模型和视图之间的粘合剂,分离每个从其他的内部工作。

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.

如果控制器是网络为中心的它必须接收其输入,并提供响应作为HTTP请求,响应。但是,如果我想打电话给我的应用程序(和获取其提供的业务)从Windows应用程序,使用RPC或其他一些东西传达什么?然后怎样呢?

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?

那么,你将不得不重写控制器,使客户端逻辑无关。但随着业务层,你已经有了。 Yyou不需要重写的东西。

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天全站免登陆