I have a customer entity. My customer entity has a collection of address value objects that pertain to my given entity. If a customer adds a new address, how will my repository know which address is the new one to add when I pass it back to the repository to update? I’m using plain ado.net.
public class Customer
{
public List<Address> Addresses { get; set; }
}
public class CustomerRepository
{
public bool Update(Customer customer)
{
//Update logic.
}
}
The truth is that it’s something complicated and that’s why one should use an ORM (EntityFramework, NHibernate, etc.) instead of reinventing the wheel.
The most easy thing I think you can do is to make your entities implement INotifyPropertyChanged and make your repository suscribe to the PropertyChanged. Than, you can have a track of the object that have changed and their properties, so you will be able to create the needed updates as well.
Also, if your collection is an ObservableCollection you will be able to track if an item was added (and make the proper insert) of if some object was removed (and make the proper delete or update if it was moved to another object, depending on the logic you expect).
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
http://msdn.microsoft.com/en-us/library/ms668604.aspx
Hope it helps.