0

私のプロジェクトでは、ドメインモデルごとに1つのビューモデルがあり、このビューモデルを複数のビューで再利用しています。

1つのビューモデルの例

 public class ProductViewModel
    {
        public int ProductId { get; set; }
        public int ProductGroupId { get; set; }
        public bool IsLinkedToErp { get; set; }

        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Standard")]
        public bool IsDefault { get; set; }

        [MaxLength(50)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Artnr")]
        public string ArtNo { get; set; }

        [MaxLength(255)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Beskrivning")]
        public string Description { get; set; }

        [MaxLength(255)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Specifikation")]
        public string Specification { get; set; }

        [MaxLength(5)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Enhet")]
        public string Unit { get; set; }

        [MaxLength(4)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Konto")]
        public string Account { get; set; }

        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Netto")]
        public decimal NetPrice { get; set; }

        public string ChUser { get; set; }

        public DateTime ChTime { get; set; }

        public string GetUpdatedDate
        {
            get { return String.Format("{0:d}", ChTime); }
        }

        public string GetNetPrice
        {
            get { return String.Format("{0:0.00}", NetPrice); }
        }
    }

ProductControllerのすべてのビューは、このViewModelを再利用します。リストの場合は、追加および編集します。しかし、リストでは、すべてのプロパティのいくつかのみを使用しています。

次に、Automapperを使用してドメインにマップします。その逆も同様です。

今、誰かがこれの経験を持っているかどうか疑問に思います。プロジェクトが成長するにつれて、このソリューションの使用に問題が生じると思いますか?

4

1 に答える 1

3

I would consider splitting out the ListViewModel and the Add/Edit view models if only because you are potentially populating and sending more data than you really need to your view.

By leveraging from inheritance, you could quickly make two classes that contain only the data that you need for your view. Based on your example, I would consider the following:

public class ProductListViewModel{
    public IEnumerable<ProductListModel> products {get;set;}

    public string SomeOtherPotentialVariableForProductList {get;set;}
}

public class ProductListModel{
    public int ProductId { get; set; }

    public int ProductGroupId { get; set; }

    [MaxLength(255)]
    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Beskrivning")]
    public string Description { get; set; }

    [Required(AllowEmptyStrings = false)]
    [Display(Name = "Netto")]
    public decimal NetPrice { get; set; }
}

public class ProductViewModel : ProductListModel
    {

        public bool IsLinkedToErp { get; set; }

        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Standard")]
        public bool IsDefault { get; set; }

        [MaxLength(50)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Artnr")]
        public string ArtNo { get; set; }

        [MaxLength(255)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Specifikation")]
        public string Specification { get; set; }

        [MaxLength(5)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Enhet")]
        public string Unit { get; set; }

        [MaxLength(4)]
        [Required(AllowEmptyStrings = false)]
        [Display(Name = "Konto")]
        public string Account { get; set; }

        public string ChUser { get; set; }

        public DateTime ChTime { get; set; }

        public string GetUpdatedDate
        {
            get { return String.Format("{0:d}", ChTime); }
        }

This accomplishes a few things.

  1. You are only sending the data that is required for the view.
  2. If you find yourself needing more information for the list view that may not be directly related to a product, you can quickly add that property.
  3. By inheriting from the ProductListModel, you are not repeating yourself within your code.
于 2013-01-11T08:39:46.610 に答える