ASP.NET MVC - 控制器作业 [英] ASP.NET MVC - Job of Controllers

查看:263
本文介绍了ASP.NET MVC - 控制器作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我开始对MVC中控制器的工作感到困惑。

I think I'm beginning to be confused with the job of a controller in MVC.

我有一个服务公开了五个函数:

I have a service that exposes five functions:


  • 列出包裹在队列中

  • 获取包裹

  • >
  • 接受包裹

  • 拒绝包裹

  • list packages in queue
  • get package
  • delete package
  • accept package
  • deny package

MVC控制器依赖于这个服务,并且通常可以对Action执行服务调用。我很高兴到目前为止。

My ASP.NET MVC controller depends on this service, and can generally execute a service call on an Action. I'm happy so far.

第二部分然后构建ViewModel结果。如果我在控制器里面这样做,控制器现在有一个爆炸的依赖列表 - 添加的每个动作增加依赖关系,以构建视图模型,这些都是由控制器继承。我不喜欢这样。我建立这个控制器,依赖于N不同的视图模型构建器,但只使用其中的每个请求之一。

The second part is then building the ViewModel result. If I do this inside of the controller, the controller now has an exploding dependency list -- each action added increases the dependencies in order to build the view model, and these are all inherited by the controller. I don't like this very much. I'm building this controller that depends on N different view model builders, but only using one of them per request.

所以我想把所有这一切,应用特定于每个视图模型的操作过滤器。我还没有这样做,但似乎还好。

So I was thinking of pulling all of this out and applying action filters specific to each view model. I haven't done this yet, but it seems okay.

这个问题对我来说是:控制器的责任是什么?如果我最终将视图模型构建为过滤器,我的控制器只是做一个路由执行服务调用(和提供一个过滤器插件)。如果我离开我的控制器负责构建每个视图模型,它变成一团糟。

The question this is raising for me is: what is the responsibility of the controller? If I end up pulling the view model building into filters, my controller is doing little more than having a route execute a service call (and providing a filter plugin). If I instead leave my controller responsible for building each view model, it becomes a mess.

似乎我几乎想要实例化一个请求,而不是一个控制器,

It seems like I almost want to instantiate an action per request, not a controller, and I'm just abusing filters to get at that?

推荐答案

你有专用的ViewModels和Poco模型吗?如果是这种情况,您可以处理ViewModel中的服务的数据。
我很高兴与这个uproach。

Do you have dedicated ViewModels and Poco-Models? If this is the case you can handle the data from the services inside the ViewModel. I am quite happy with this uproach.

public class PackageViewModel()
{
    public PackageDetail{get;set;}
    public int PackageID{get;set;}
    public List<Package>Packages{get;set;}
    public string SomeFilterCriteria{get;set;}

    publlic void FillPackageList(IPackageService packageService)
    {       
        Packages = packageService.GetPackages(SomeFilterCriteria);      
    }
}

在控制器中:

public ViewResult ListPackages(PackageViewModel model)
{
    model.FillPackageList(PackageService);
    return View("ListPackages",model);

}



我不明白你的意思是 。

I dont understand what you mean by "view model builders".

这篇关于ASP.NET MVC - 控制器作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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