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

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

问题描述

据我所知,MVC 通过作为控制器的胶水"将类定义(模型)与表示(视图)分开.控制器应该有一个单一的责任,因此是可测试的.ViewModel 用于将来自多个实体的数据汇集在一起​​,并为视图按摩"来自控制器的数据.

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 therefore 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 应用程序时使用服务层.它类似于 Martin Fowler 在企业应用程序架构模式中讨论的服务层模式.它封装了您的业务逻辑并使控制器非常精简.基本上,控制器使用服务层来获取域模型,然后将其转换为视图模型.我还使用工作单元设计模式来处理事务和<一个 href="http://msdn.microsoft.com/en-us/library/ff649690.aspx" rel="noreferrer">Repository Design Pattern 来封装数据访问层以便于单元测试和能够轻松更换 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天全站免登陆