在ASP.NET MVC保湿的ViewModels [英] Hydrating ViewModels in ASP.NET MVC

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

问题描述

我有一个由许多用户控件的页面。该页面的浏览模式是相当复杂的。

I have a page that is made up of many user controls. The view model for this page is rather complex.

public class ComplexViewModel
{
    public ObjectA ObjectAProperty { get; set; }
    public List<Things> ListOfThings { get; set; }
    public List<ThingCategories> ListOfThingCategories { get; set; }
    public List<ThingTypes> ListOfThingTypes { get; set; }
    public List<ThingOptions> ListOfThingOptions { get; set; }
    public int ChosenThingCategoryId { get; set; }
    public int ChosenThingTypeId { get; set; }
    public int ChosenThingOptionId { get; set; }
    public OtherObject ObjectData { get; set; }
}

本页面还包含过滤的信息,整理的PostModel等。

This page also has an PostModel that contains information for filtering, sorting, etc.

    public class SimplePostModel
{
    public int ChosenThingCategoryId { get; set; }
    public int ChosenThingTypeId { get; set; }
    public int ChosenThingOptionId { get; set; }
    public int ChosenThingFilterTypeId { get; set; }
    public int ChosenThingSortTypeId { get; set; }
    public int ChosenThingOtherId { get; set; }
    public int ChosenThingMoreId { get; set; }
    public int ChosenThingOMGId { get; set; }
}

简单PostModel进行验证,然后控制器打开3+库使得多个调用到每个并构建视图模型。至少可以说,我的控制器操作已经变得相当大。

The simple PostModel is validated and then the controller opens 3+ repositories making multiple calls into each and builds the view model. To say the least my controller action has gotten quite large.

这是迄今为止最复杂的页面,我的工作,我有一个很难决定如何使它更简单。

This is by far the most complex page I've worked on and I'm having a hard time deciding how to make it simpler.

我首先想到的是要建立一个视图模型工厂,绑定验证后,会调用到库,并返回视图模型。

My first thought was to create a view model factory that, after binding validation, would call into the repositories and return the ViewModel.

然后我想到了创建,将验证PostModel然后滋润视图模型在一步一个自定义模型粘合剂。

Then I thought about creating a custom model binder that would validate the PostModel and then hydrate the ViewModel in one step.

所以我的问题是你如何滋润一个复杂的视图模型?

虽然我写这篇文章我已经使用Html.RenderAction和创造为每个用户控件,使一个页面的此兽模型的想法。

And while I write this I had the idea of using Html.RenderAction and creating a model for each of the user controls that make up this beast of a page.

更新:

的储存库进行呼叫进入的WCF服务,应用程序是一个更大的SOA拱的一部分。

The repositories make calls into WCF services, the application is part of a larger SOA arch.

推荐答案

一些通用的技巧。数据可以分为几类:系统范围,会话范围,请求范围的

Some general tips. Data can be separated into several categories: system-scope, session-scope, request-scope.

系统范围的数据是需要被psented用户$ P $但是每个用户的相同的数据。对于博客应用程序的一个例子是一个标签云,或类别的列表。我认为,这个数据并不需要流经控制器或行动,因为它无关,与用户交互。它知道如何让自己可以调用的HtmlHelper(或LayoutDataHelper)的视图(和preferably缓存)此数据。

System scope data is data that needs to be presented to the user but is the same for each user. An example for a blog application would be a tag cloud, or a list of categories. I would argue that this data doesn't need to flow through the controller or action because it has nothing to do with user interaction. The View itself can call a HtmlHelper (or a LayoutDataHelper) that knows how to get (and preferably cache) this data.

会话范围的数据可以与填充在ViewData.Model领域ActionFilters处理。它不直接相关的动作的参数。例如,用户名。我preFER形式的属性。

Session scope data can be handled with ActionFilters that populate fields on the ViewData.Model. It is not directly related to the parameters of the action. For example, username. I prefer an attribute of the form

public class SessionDataFilter : ActionFilter 
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Result is ViewResult)
        {
            var view = filterContext.Result as ViewResult;

            // hydrate session data on view.ViewData.Model
        }
    }
}

其他一切,是请求范围/特定必须在行动进行填充。但是,这并不意味着你必须有一个大规模行动的方法来做到这一点。我想看看你的ViewModels是如何构成的。如你所说,如果您有需要填充的控制,很可能是在视图模型的信息可以分为相关sets.So你可能有一个视图模型只是构成其他较小的视图模型(局部视图模式)。然后我会分解逻辑每个部分的视图模式(以及任何其他复杂的逻辑),每个填充到其自己的可重复使用的隔离方法。

Everything else that is request-scope/specific must be populated in the Action. However, this doesn't mean you have to have one massive action method to do it. I would look at how your ViewModels are composed. As you suggested, if you have controls that need populating, it's likely that the information in the ViewModel can be grouped into related sets.So you might have a ViewModel that just composes other smaller view models ("partial view models"). I would then decompose the logic to populate each partial view model (and any other complex logic) each into its own re-usable and isolated method.

类似的抽象与职位打交道时适用,虽然我会担心的那个帖子大量不相关的数据页面的可用性。您应该能够使用 ActionFilters OnActionExecuting )来解析输入数据的相关集(和可选验证),并分配他们动作参数。我美元的粘合剂p $ PFER滤波器公布的数据,除非同一组数据的发布到多个操作和输入数据的形状始终是相同的。

Similar abstraction applies when dealing with posts, though I would worry about the usability of pages that post lots of unrelated data. You should be able to use ActionFilters (OnActionExecuting) to parse related sets of incoming data (and optionally validating) and assign them to action parameters. I prefer filters over binders for posted data unless the same set of data is posted to multiple actions and the shape of the incoming data is always the same.

祝你好运。

这篇关于在ASP.NET MVC保湿的ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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