I follow this tutorial on WPF MVVM
https://metanit.com/sharp/wpf/21.2.php
It's in russian, but basically the viewmodel contains a list of Phone
objects and the tutorial teaches how to implement Add/Remove/Delete Commands to the list of Phone
with writing the changes to the database. Here's the EditCommand
Command:
public RelayCommand EditCommand
{
get
{
return editCommand ??
(editCommand = new RelayCommand((selectedItem) =>
{
if (selectedItem == null) return;
Phone phone = selectedItem as Phone;
Phone vm = new Phone()
{
Id = phone.Id,
Company = phone.Company,
Price = phone.Price,
Title = phone.Title
};
PhoneWindow phoneWindow = new PhoneWindow(vm);
if (phoneWindow.ShowDialog() == true)
{
phone = db.Phones.Find(phoneWindow.Phone.Id);
if (phone != null)
{
phone.Company = phoneWindow.Phone.Company;
phone.Title = phoneWindow.Phone.Title;
phone.Price = phoneWindow.Phone.Price;
db.Entry(phone).State = EntityState.Modified;
db.SaveChanges();
}
}
}));
}
}
Here, author copies properties one by one from selectedItem
to the vm
object and later from phoneWindow
to the database model.
The problem is, in the project I'm working on, instead of a simple class Phone
I use complex class with many nested types and nested lists. Do I have to copy them all by hand 2 times? How can I automate it?