我正在设计一个应用程序,其中类似的实体在两个地方有不同类型的集合,如下所示。通用接口
型号:
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; }
}
and classes will将
class PersonModel<List> {}
class personViewModel<ObservableCollection> {}
但编译器没有准备好编译我的接口。 :( 说,类型参数“T”不能与类型参数一起使用。
原因,我想这一点,我想尽量减少类型转换/到&视图模型建模。
我的视图模型将会怎样对此,
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>(); }
}
}
建议我更好的办法,有型号&视图模型同步。
也许你应该只使用Automapper。 – CodesInChaos 2012-02-02 09:38:42
我已经使用它,但团队成员不喜欢它的大和嵌套实体 – hungryMind 2012-02-02 09:40:36