1

我正在设计一个应用程序,其中类似的实体位于两个不同类型的集合中,如下所示。

模型:

class PersonModel {
  public string Name { get;set;}
  public List<Address> Addresses { get;}
  public List<OtherType> OtherTypes { get;}
}

类似的视图模型:

class PersonViewModel {
   public string Name { get;set;}
   public ObservableCollection<Address> Addresses { get; }
   public ObservableCollection<OtherType> OtherTypes { get; }
}

为了使两个实体保持一致,我想使用通用接口来确保两者都实现所有属性,所以我创建了这样的东西:

public interface IPerson<T> where T: ICollection<T> {
   string Name { get;set;}
   T<Address> Addresses { get;}
   T<OtherType> OtherTypes [ get; } 
}

和课程将是

class PersonModel<List> {}
class personViewModel<ObservableCollection> {}

但编译器还没有准备好编译我的界面。:( 说,类型参数“T”不能与类型参数一起使用。

我想要这个的原因,我想最小化从/到模型和视图模型的类型转换。

我的 viewModel 会是这样的,

class PersonViewModel<T> : IPerson<T> {
   public PersonViewModel(IPerson model){
      this.Model = model;
   }
   internal PersonModel Entity {
      get; set;
   }
   public string Name { 
      get{ return model.Name;} 
      set {model.Name = value;}
   }
   public T<Address> Addresses {
      get { return model.Addresses.Cast<T>(); }
   }
}

建议我更好地让模型和视图模型同步。

4

4 回答 4

1

ViewModel 的存在是为了为 View 提供数据。这意味着它应该按照视图的要求建模。通常,这些要求与模型的要求不同。这意味着,通常,您的模型和视图模型不会同步,它们会有所不同。在您的方法中, ViewModel 没有增加任何价值,可以删除。

要在 ViewModel 和 Model 之间进行映射,您可以使用AutoMapper

于 2012-02-02T09:38:56.873 回答
1

您的实现应如下所示:

class PersonModel : IPerson<List> {}
class PersonViewModel : IPerson<ObservableCollection> {}

你真的需要一个泛型类吗?ObservableCollection<T>并且List<T>两者都实现了ICollection<T>,因此您可以在接口中分别声明 Addresses 和ICollection<Address>OtherTypes ICollection<OtherType>

(什么是地址视图?)

于 2012-02-02T09:41:16.683 回答
0

您不能以这种方式使用泛型。你可以试试这样的

public interface IPerson
{
    string Name { get; set; }
    ICollection<Address> Addresses { get; }
    ICollection<OtherType> OtherTypes { get; }
}

public class OtherType { }
public class Address { }

接着

class PersonModel : IPerson
{
    public PersonModel()
    {
        Addresses = new List<Address>();
        OtherTypes = new List<OtherType>();
    }

    public string Name { get; set; }
    public ICollection<Address> Addresses { get; private set; }
    public ICollection<OtherType> OtherTypes { get; private set; }
}

class PersonViewModel : IPerson
{
    public PersonViewModel()
    {
        Addresses = new ObservableCollection<Address>();
        OtherTypes = new ObservableCollection<OtherType>();
    }

    public string Name { get; set; }
    public ICollection<Address> Addresses { get; private set; }
    public ICollection<OtherType> OtherTypes { get; private set; }
}
于 2012-02-02T09:47:12.673 回答
0

您对的通用约束IPerson是强制 T 必须实现ICollectionT 的一个?这是无休止的递归,这是不允许的。

您也不能在泛型类型上指定泛型参数,所以T<Address>不允许,这是因为不知道是否T是泛型类型。

您可以将界面更改为以下内容:

public interface IPerson<TAddressCol, TOtherCol> 
   where TAddressCol: ICollection<Address>
   where TOtherCol : ICollection<OtherType> 
{
   string Name { get;set;}
   TAddressCol Addresses { get;}
   TAddressCol OtherTypes [ get; } 
}

然后像这样使用它:

class PersonModel<List<Address>, List<OtherType>> {}
class personViewModel<ObservableCollection<Address>, ObservableCollection<OtherType>> {}

I think that's the only real way to get the approach you want. But I would suggest that your interface merely returns ICollection<Address> and ICollection<OtherType>. Your model/viewmodel will then have to expose the collections through the interface, however there is nothing stopping you having the backing implementations as List or ObservableCollection respectively.

于 2012-02-02T09:49:14.927 回答