我正在設計一個應用程序,其中類似的實體在兩個地方有不同類型的集合,如下所示。通用接口
型號:
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