4

好的,基本问题,我是一名自学成才的开发人员,所以我似乎经常无法确定哪种方法是正确的方法......这就是其中之一!简单我有一个视图模型,其中包含一组子项。但是在定义这些类的地方我无法决定子对象是否应该是父对象的子类......

例如这个:

public class ActionChartViewModel
{
    public IEnumerable<ActionChartItemViewModel> Items { get; set; }
    public TextPagingInfo TextPagingInfo { get; set; }
}

public class ActionChartItemViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Rating { get; set; }
    public string Comment { get; set; }
    public string AssignedToUserName { get; set; }
    public string ContactRequested { get; set; }
    public bool Resolved { get; set; }
    public int NoteCount { get; set; }
    public string ContactDetails { get; set; }
    public int ResponseId { get; set; }
}

或这个:

public class ActionChartViewModel
{
    public IEnumerable<Item> Items { get; set; }
    public TextPagingInfo TextPagingInfo { get; set; }

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Rating { get; set; }
        public string Comment { get; set; }
        public string AssignedToUserName { get; set; }
        public string ContactRequested { get; set; }
        public bool Resolved { get; set; }
        public int NoteCount { get; set; }
        public string ContactDetails { get; set; }
        public int ResponseId { get; set; }
    }
}

我更喜欢第二个代码可读性和简单性,但我不知道子类的优缺点。大家会怎么想??

提前致谢!!

4

2 回答 2

3

我会使用单独的类(在同一个文件中)而不是内部类。当内部类仅服务于父类时,内部类将很有用,即不会从父类外部访问,只能通过父类方法等访问。在您的情况下,内部类需要在视图上使用,所以我认为没有必要。第一个选项,即单独的类,实际上对我来说更简单并且读起来更好。

于 2013-02-07T01:36:01.850 回答
1

“子类”是指您创建其类型的更具体的实现(继承)。正如@bloparod 所说,您正在做“内部课程”。我也很少使用内部类。有时我使用 some privateor internalclass 作为临时的。如果你这样做,你将需要使用 sintaxe 来创建,例如:

ActionChartViewModel.Item item = new ActionChartViewModel.Item(); 

我通常将文件分开并使用public类,但有时当我有很多 ViewModel 时,我认为一个好的做法是将所有相同类别的 ViewModel 保存在一个文件中,并在必要时继承,例如:

文件: ProductViewModel.cs

public class ProductViewModel 
{
   public int Id { get; set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
   public string CategoryName { get; set; }
}

public class ProductDetailViewModel : ProductViewModel 
{
   public int Stocke { get; set; }
   public string Obs { get; set; }
   public IEnumerable<ProductMovViewModel> Inventory 
   /* other properties */
}

public class ProductMovViewModel 
{
   public int Id { get; set; } 
   public DateTime Date { get; set;
   public int Amout { get; set; }
}

作为一个很好的做法,您也可以根据自己的喜好将您的 ViewModel 文件分开。

于 2013-02-07T01:28:57.797 回答