我正在设计一个应用程序,其中类似的实体位于两个不同类型的集合中,如下所示。
模型:
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>(); }
}
}
建议我更好地让模型和视图模型同步。