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

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

问题描述

我觉得我开始用MVC控制器的工作相混淆。

我有暴露五大功能服务:


  • 在队列列表包

  • 获得包

  • 删除软件包

  • 接受包

  • 否认包

我的ASP.NET MVC控制器依赖于该服务,并且通常在一个Action的execute拨打服务电话。我很高兴这么远。<​​/ P>

第二部分是要建设视图模型的结果。如果我这样做控制器内,控制器现在有一个爆炸的依赖列表 - 添加的每个动作增加,以建立视图模型的依赖,而这些都是由控制器继承。我不喜欢这种非常多。我建立这个控制器取决于N个不同的视图模型建设者,但是只能使用每个请求的其中之一。

所以我想拉这一切并运用行动具体到每一个视图模型过滤器。我没有做这个还没有,但似乎还行。

这是提高我的问题是:什么是控制器的责任?如果我最终拉动视图模型筑成的过滤器,我的控制器是做多一点有一个路线执行服务调用(和提供过滤插件)。如果我不是把我的控制器负责建立每个视图模式,它变得一团糟。

好像我几乎要实例每个请求,而不是一个控制器的动作,而我只是滥用过滤器,以获得该?


解决方案

您是否有专门的ViewModels和波科的模型?如果是这样的情况下,可以处理从视图模型内的服务的数据。
我这个uproach非常高兴。

 公共类PackageViewModel()
{
    公共PackageDetail {获取;设置;}
    公众诠释包标识{获取;设置;}
    公开名单&LT;包装及GT;套餐{获得;设置;}
    公共字符串SomeFilterCriteria {获取;设置;}    publlic无效FillPackageList(IPackageService packageService)
    {
    包= packageService.GetPackages(SomeFilterCriteria);
    }
}

在控制器:

 公开的ViewResult ListPackages(PackageViewModel模型)
{
    model.FillPackageList(PackageService);
    返回视图(ListPackages模型);}

我不明白你所说的视图模型建设者的意思。

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

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

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?

解决方案

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);		
    }
}

In Controller:

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