通用视图模型? [英] Generic View Models?

查看:137
本文介绍了通用视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道它是很好的做法,力图使一个观点,即发生在一个普通视图模型?

I am wondering is it good practice to try to make a view that takes in a generic view model?

我想知道这一点,因为有人提到,他期待有做大量的重复code,除非他开始做一个通用视图和通用视图模型。

I am wondering this because someone mentioned that he was anticipating to have to do lots of duplicate code unless he started to make a generic view and generic view model.

所以基本上的意见会像只一组控件。一种观点可能有2控件(比如一个文本框和单选按钮),另一种观点认为可能会对这50控制。

So basically the views would be like just a set of controls. One view might have 2 controls(say a text-box and radio button) another view might have 50 controls on it.

它们将都具有相同的外观和感觉(它只是由控制数量的增长)。基本上,他想有一个视图模型需要在对象(域对象)看着它,看到的50场,并呈现正确的控制类型。

They will all have the same look and feel(it just grows by number of controls) . Basically he was thinking having a view model takes in the object(domain object) looks at it and see's 50 fields and renders the right control types.

我猜编辑模板可以用来找出但是我只是不在一个通用视图模式销售的控制。

I guess a edit template could be used to figure out the controls however I am just not sold on a generic view model.

我喜欢泛型和他们可以做的非常强大的东西,在某些情况下,他们是好的,但我只是不全面,以疯狂的他们,尽量不要使用。

I like generics and they can do very powerful things and in some situations they are good but I am just not overall to crazy about them and try to not use.

我觉得大部分时间它可能会减少重复code,但有时它使code很多更复杂。当然,这可能只是因为我还是一个比较新的节目,它可能是仍高于我的技能水平。

I find most of the time it may reduce duplicate code but sometimes it makes the code alot more complicated. Of course this could just because I am still a relatively new to programming and it could be still above my skill level.

接下来的问题,我与它是我认为视图模型应该尽可能平坦,仅暴露,实际上是将要使用的数据,这样的人不开始使用应该从来没有在视图中的属性第一位。

The next problem I have with it is I think that view models should be as flat as possible and only expose data that is actually going to be used so people don't start using properties that should never been in the view in the first place.

接下来的问题,我与它,它可能只是继续下去,如果你有一些复杂的对象,它有它的对象中有对象。它可以走了很长很长时间。

The next problem I have with it that it could just keep going if you have some complex object that has objects in it that has objects in it. It could go for a long long time.

推荐答案

我看不出什么毛病通用的ViewModels。这是消除重复和保持编译时检查,而不是ViewBag的好办法。

I don't see anything wrong with generic ViewModels. It is a good way to remove duplication and keep compile-time checks, as opposed to ViewBag.

示例:

假设你有产品类别等一组模型类
每个类(产品型号 CategoryModel )有一个关联的显示和编辑模板,生成相应的视图。

Imagine you have a set of Model classes for Product, Category, etc. Each class (ProductModel, CategoryModel) has an associated display and editor template, which generates appropriate view.

现在要构建一套用于查看和编辑网页。

Now you want to construct a set of pages for view and edit.

我通常创建一个布局(母版页中的网页形式)呈现共同的内容(页眉,页脚,菜单等)

I usually create a Layout (Master page in web forms) to render the common content (header, footer, menu, etc.)

然后我会创建一个接受为模型的个体,强类型的意见 ProductViewModel CategoryViewModel

Then I would create individual, strongly-typed views that accept as model ProductViewModel, CategoryViewModel, etc.

现在,我们需要定义这些视图模型类。每个视图模型类应产品型号 CategoryModel 的一个实例,等等(这将被传递到模板)。但布局常常需要一些附加的数据(即选择的菜单,登录用户名等)。我的解决方案是创建一个通用的视图模型,封装此重复数据的布局:

Now we need to define those view model classes. Each view model class should take an instance of ProductModel, CategoryModel, etc (which will be passed to the template). But the layout often requires some additional data (ie. selected menu, logged-in user name, etc). My solution is to create a generic ViewModel that encapsulates this duplicate data for the Layout:

public class EntityViewModel<T>
    where T : EntityModel
{
    public T Entity { get; set; }
    public string UserName { get; set; }
    public string SelectedMenu { get; set; }
}

然后你就可以轻松地创建一个 ProductViewModel:EntityViewModel&LT;产品型号&GT; ,其中包含的一切布局需要渲染页面,并且可以添加有任何额外的,产品 - 具体的数据。

Then you can easily create a ProductViewModel : EntityViewModel<ProductModel>, which contains everything the Layout need to render the page, and you can add there any additional, product-specific data.

这篇关于通用视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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