Do you think this Model-ViewModel implementation is correct? Note that Stars (in the VM) is an ObservableCollection of VMs inside another one, and i feel like it decouples the VM from the M because when removing an element from Stars, i still have to delete it's model manually.
Any ideas on how to improve this without using OnCollectionChanged? Thanks in advance.
Models:
public class Galaxy
{
public Galaxy(string name, IList<Star> stars)
{
Name = name;
Stars = stars;
}
public string Name { get; set; }
public IList<Star> Stars { get; set; }
}
public class Star
{
public Star(string name)
{
Name = name;
}
public string Name { get; set; }
}
ViewModels :
public class GalaxyVM : ViewModelBase
{
private Galaxy _galaxy;
private ObservableCollection<StarVM> _stars;
public GalaxyVM(Galaxy galaxy)
{
_galaxy = galaxy;
_stars = new ObservableCollection<StarVM>(from sys in _galaxy.Stars
select new StarVM(sys));
}
public string Name
{
get { return _galaxy.Name; }
}
public ObservableCollection<StarVM> Stars
{
get { return _stars; }
}
}
public class StarVM : ViewModelBase
{
private Star _star;
public StarVM(Star star)
{
_star = star;
}
public string Name
{
get { return _star.Name; }
}
}