创建我的MVC应用服务层? [英] Creating a Service Layer for my MVC application?

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

问题描述

据我所知,MVC通过胶水,也就是控制器分离从presentation(视图)的类定义(模型)。该控制器应该有一个责任,并为此被测试。的ViewModels用于从多个实体汇集数据,并以按摩从控制器的视图中的数据。

From what I understand, MVC separates the class definitions (model) from the presentation (view) via the "glue" that is the controller. The controller should have a single responsibility and therefor be testable. ViewModels are used to bring together data from multiple entities and to "massage" the data from the controller for the view.

这似乎是商业逻辑并没有真正有一个地方......所以我想对服务的另一层将是合适的。我只是不知道在什么地方这一层,或如何构建的服务 - 它应该是一个名为服务的类,它包含了一堆的功能呢?我是一个有点新的MVC,所以提示的阅读材料,样品或一般的新人那种将是真棒。

It seems like business logic doesn't really have a place... so I'm thinking another layer for services would be suitable. I'm just not sure where to place this layer, or how to build the services - should it be a class called "services" that contains a bunch of functions? I'm a bit new to MVC, so any reading material, samples, or general newcomer kind of tips would be awesome.

推荐答案

我开发ASP.NET MVC应用程序时,通常使用一个服务层。它类似于服务层图案的马丁·福勒在企业应用架构的的的模式探讨。它封装了业务逻辑,使控制器pretty薄。基本上,控制器使用服务层,以获得域模型那么转化为视图模型。我也用工作设计模式的部门处理事务和的库设计模式的封装,方便单元测试数据访问层,并能轻松更换ORM的。这一数字说明,我在MVC应用程序使用典型的层。

I usually use a Service Layer when developing ASP.NET MVC application. It is similar to the Service Layer Pattern that Martin Fowler discusses in Patterns of Enterprise Application Architecture. It encapsulates your business logic and make the controllers pretty thin. Basically the controllers use the service layer to get the domain models that are then transformed into view models. I also use the Unit of Work Design Pattern to handle transactions and the Repository Design Pattern to encapsulate the data access layer for easier unit testing and being able to easily swap out ORM's. This figure shows the typical layers that I use in an MVC application.

服务层标记为这个图,因为我发现人眼花缭乱,当你使用服务层中的应用程序或领域层。他们倾向于认为这是一个Web服务。它实际上是可以通过自己喜欢的网络服务技术,使用如的ASP.NET Web API或WCF,以及控制器组件。

The service layer is labeled as the "Application or Domain Layer" in this diagram because I find people get confused when you use the term "Service Layer". They tend to think that this is a web service. It is actually an assembly which can be used by your favorite web service technology, such as ASP.NET Web API or WCF, as well as a controller.

至于命名约定,我通常使用的东西描述域名,后服务。例如,如果我有一个处理用户会员服务层,然后我将有一个名为MembershipService类具有所有由控制器和和Web服务来查询和处理会员域所需要的方法。注意:您可以在同一个应用程序的多个领域,所以你可以有多个服务层。我的观点是在这里,你不必有一个整体的服务,需要整个应用程序的服务。

As for naming conventions I usually use something that describes the domain followed by service. For example, If I have a service layer that handles user membership then I would have a class called MembershipService that has all of the methods needed by controllers and and web services to query and manipulate the membership domain. Note you may have several domains in the same application so you can have multiple service layers. My point being here that you do not have to have one monolithic service that takes care of the whole application.

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

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