I am designing an app where similar entities are in two places with different types of collection as following.
Model:
class PersonModel {
public string Name { get;set;}
public List<Address> Addresses { get;}
public List<OtherType> OtherTypes { get;}
}
Similar view model:
class PersonViewModel {
public string Name { get;set;}
public ObservableCollection<Address> Addresses { get; }
public ObservableCollection<OtherType> OtherTypes { get; }
}
To make both entities consistent, I thought use generic interface which ensure both implement all properties, so I created something like this:
public interface IPerson<T> where T: ICollection<T> {
string Name { get;set;}
T<Address> Addresses { get;}
T<OtherType> OtherTypes [ get; }
}
and classes will be
class PersonModel<List> {}
class personViewModel<ObservableCollection> {}
but compiler not ready to compile my interface. :( Says, the type parameter "T" cannot be used with type argument.
Reason why I want this, i wanted to minimize type conversion from / to model & viewModel.
My viewModel will be like this,
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>(); }
}
}
Suggest me better way to have Model & ViewModel synchronized.