你是如何填充/验证您的ViewModels? [英] How are you populating/validating your ViewModels?

查看:105
本文介绍了你是如何填充/验证您的ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇所有的各种方式的人也建立了自己的ViewModels以及他们为什么选择这种方法。

I'm curious of all of the various ways people are building their ViewModels and why they choose that method.

在这里我可以想到的几种方法:

I can think of several ways here:

-1。注入库 - 控制器加载模型和地图视图模型。这里的视图模型构造可采取各种馆藏interally为前设置。在选择列表中,例如:

-1. Injected repository - the controller loads the model and maps to the ViewModel. Here the ViewModel constructor could take various collections to interally set for ex. in a select list such as:



public CustomerController(ISomeRepository repository)
{
   _repository = repository;
}

public ActionResult Create()
{
  CustomerCreateViewModel model = new CustomerCreateViewModel(_repository.GetShipTypes, 
                                                                _repository.GetStates);
..
..
}

-2。 ViewModelBuilder - 无论是注射或控制器与注入的资源库的一个实例实例化。通过一些所谓的像

-2. ViewModelBuilder - Either injected or instantiated in the controller with an instance of the injected repository. Called via something like

>var orderViewModel = orderViewModelBuilder.WithStates().Build(orderId);

var orderViewModel = orderViewModelBuilder.WithStates().Build(orderId);

-3。直接在控制器(不需要code - 其凌乱)

-3. Directly in controller (no code required - its messy)

-4。返回域模型,控制器将映射或视图模型的一些其他服务(注射或不)(任何人都这样做,以返回未明确指定的视图模型/视为一个视图模型生成器类?)

-4. Some other service (injected or not) that returns domain model which the controller then maps or a ViewModel (anyone doing this to return a view model that isn't specifically named/noted as a ViewModel builder class?)


public JobCreateViewModel BuildJobCreateViewModel(int parentId)
{
   JobCreateViewModel model = new JobCreateViewModel();
   model.JobStatus = _unitOfWork.JobRepository.GetJobStatuses();
   model.States=_unitOfWork.StateRepository.GetAll();
   return model;
}

现在回程 - 关于验证您的视图模型 - (前数据注解属性),您在一个基ViewModel类所有的ViewModels之间继承了标准验证,或复制您的验证,或者单纯依靠服务器端验证所以都可以againt你的域对象验证?

Now on the return trip - regarding validating your view models - are you inheriting from a base ViewModel class for standard validations, or copying your validations (ex. data annotation attributes) between all of your ViewModels, or simply relying on server side validation so it can all be validated againt your domain object?

任何其他方面?更好的东西?为什么呢?

Any others? Anything better? Why?

修改
根据下面的链接,我没有找到吉米·博加德上的ViewModels的体系结构的好文章。虽然它没有上述直接解决的问题,这是任何人来这里的ViewModel信息有很大的参考。
<一href=\"http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/\">http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/

推荐答案

我注入一个服务到控制器,而不是一个仓库,然后用的 AutoMapper 把它转换成一个视图模型。在这种情况下,服务层的好处是,它可以聚集来自一个或多个库的多个简单的操作到单个操作中暴露域模型。例如:

I inject a service into the controller, not a repository, and then use AutoMapper to convert it into a view model. The benefit of the service layer in this case is that it could aggregate multiple simple operations from one or more repositories into a single operation exposing a domain model. Example:

private readonly ICustomerService _service;
public CustomerController(ICustomerService service)
{
    _service = service;
}

[AutoMap(typeof(Customer), typeof(CustomerViewModel))]
public ActionResult Create(int id)
{
    Customer customer = _service.GetCustomer(id);
    return View(customer);
}

在这个例子中自动地图是一个自定义操作过滤器,我可以写一个控制器动作后执行,检查返回的对象,并使用定义AutoMapper映射将其映射到指定的目标类型。因此,认为得到相应的CustomerViewModel的型号。本来等同于:

in this example AutoMap is a custom action filter that I can write which executes after the controller action, inspects the returned object and uses defined AutoMapper mappings to map it to the specified destination type. So the view gets the corresponding CustomerViewModel as model type. Would have been equivalent to:

public ActionResult Create(int id)
{
    Customer customer = _service.GetCustomer(id);
    CustomerViewModel vm = Mapper.Map<Customer, CustomerViewModel>(customer);
    return View(vm);
}

这只是实在是太多了管道和重复code可能被集中。

it's just that it is too much plumbing and repetitive code that could be centralized.

我也建议你看把你的控制器节食视频从吉米·博加德。

I would also recommend you watching the putting your controllers on a diet video from Jimmy Bogard.

这篇关于你是如何填充/验证您的ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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