应该在服务层返回查看型号为MVC应用程序? [英] Should a service layer return view models for an MVC application?

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

问题描述

假设你有一个ASP.NET MVC项目,并使用一个服务层,如在asp.net网站上的这个联系人管理器教程:<一href=\"http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs\">http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs

Say you have an ASP.NET MVC project and are using a service layer, such as in this contact manager tutorial on the asp.net site: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs

如果您的ViewModels为您的看法,是服务层中的适当位置,以提供每个视图模型?例如,服务层code样品中存在的方法

If you have viewmodels for your views, is the service layer the appropriate place to provide each viewmodel? For instance, in the service layer code sample there is a method

    public IEnumerable<Contact> ListContacts()
    {
        return _repository.ListContacts();
    }

如果不是你想要一个IEnumerable的,它应该走在服务层,还是有别的地方,它是正确的地方?

If instead you wanted a IEnumerable, should it go in the service layer, or is there somewhere else that is the "correct" place?

也许更恰当,如果您有与相关的ContactController每个视图的单独视图模型,应该ContactManagerService有一个单独的方法来回报每视图模型?如果服务层是不正确的地方,应该在哪里视图模型对象由控制器初始化使用?

Perhaps more appropriately, if you have a separate viewmodel for each view associated with ContactController, should ContactManagerService have a separate method to return each viewmodel? If the service layer is not the proper place, where should viewmodel objects be initialized for use by the controller?

推荐答案

一般情况下,没有。

查看模型意图和从视图提供的信息和应特定于应用程序,而不是一般的域。控制器应该协调互动与仓库,服务(我正在这里服务的定义的一些假设)等和处理建筑和验证视图模型,并且还含有确定的观点来呈现的逻辑。

View models are intended to provide information to and from views and should be specific to the application, as opposed to the general domain. Controllers should orchestrate interaction with repositories, services (I am making some assumptions of the definition of service here), etc and handle building and validating view models, and also contain the logic of determining views to render.

跑冒滴漏视图模型变成了服务的图层,你是模糊图层,现在有可能的应用和presentation具体什么应该关注与域级别的责任混在一起。

By leaking view models into a "service" layer, you are blurring your layers and now have possible application and presentation specific mixed in with what should focused with domain-level responsibilities.

这篇关于应该在服务层返回查看型号为MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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