ASP.NET MVC使用ViewModels的最佳方法是什么?

时间:2012-10-15 08:31:03

标签: asp.net-mvc viewmodel

Customers.cs

public partial class Customers
{
    public int sno { get; set; }
    public string CustomerName { get; set; }
    public string CustomerNo { get; set; }
    ...
    // 20 more attribute too...
}

Cities.cs

public partial class Cities
{
    public int sno { get; set; }
    public string CityName { get; set; }
    public string CityPlate { get; set; }
    public string CityPhoneCode { get; set; }
}

AddCustomerViewModel.cs

public class AddCustomerViewModel
{

    [Required(ErrorMessage = "Şehir seçiniz.")]
    [Display(Name = "Şehir")]
    public Nullable<int> CityId { get; set; }

    // same with Customers.cs
    public int sno { get; set; }

    [Required(ErrorMessage = "Müşteri adını giriniz!")]
    [Display(Name = "Müşteri Adı")]
    public string CustomerName { get; set; }

    [Required(ErrorMessage = "Müşteri numarası giriniz!")]
    [Display(Name = "Müşteri Numarası")]
    public string CustomerNo { get; set; }
    ...
    // 20 more attribute too...
}

控制器

[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer()
{
    AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel();
    addCustomerViewModel.Cities = entity.Cities;

    return View(addCustomerViewModel);
}

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
{
    entity.Customers.Add(GetCustomerFromViewModel(addCustomerViewModel));
    entity.SaveChanges();

    return View(addCustomerViewModel);
}

我正在使用一个名为GetCustomerFromViewModel的函数将addCustomerViewModel转换为Customer,如下所示:

GetCustomerFromViewModel()

private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
{
    Customers customer = new Customers();

    customer.CityId = addCustomerViewModel.CityId;
    customer.CreatorUserId = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
    customer.CustomerName = addCustomerViewModel.CustomerName;
    customer.CustomerNo = addCustomerViewModel.CustomerNo;
    customer.Description = addCustomerViewModel.Description;
    ...
    // 20 more attribute too...

    return customer;
}

但是,Customers类有太多的变量(customerNo,CustomerName,...),所以这是不好的方法。

当我使用DbContextGenerator并向dataAnnotations添加类时,然后当我使用模型时,将删除dataAnnotations。 (因为DbContext类也会更新)

如何将ViewModels与DataAnnotations一起使用。和Db的有效插入操作?文章,教程,范例或建议?

我希望我能解释一下。

非常感谢...

1 个答案:

答案 0 :(得分:3)

您可以查看AutoMapper,它将简化您的域模型和视图模型之间的映射逻辑,这样您就不需要手动映射每个属性。除此之外,您的代码没有任何问题。您已经在使用视图模型并具有映射层。因此,您的GetCustomerFromViewModel功能可能会变为:

private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel)
{
    return Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
}

或完全摆脱它并在控制器动作中直接使用AutoMapper调用,因为此函数不再带来太多价值:

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel)
{
    var customer = Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel);
    entity.Customers.Add(customer);
    entity.SaveChanges();

    return View(addCustomerViewModel);
}