I am currently working with EnterpriseLibrary 5.0 and MVVM:
I have an ObservableCollection ListCategories<Category> property binded to an editable ComboBox (I can add/delete/edit categories):
I have the following code:
public ObservableCollection<Category> ListCategories
{
get
{
return listCategories;
}
set
{
listCategories = value;
}
}
var categories = sdb.ExecuteSprocAccessor <Category> ("Get_Categories_List");
ListCategories = categories.ToObservableCollection <Category>();
My question:
After all changes made in the collection, how to update back the database?
Thanks
The proper way is to have a DB Access layer behind the Repository pattern of:
then implement this with your domain type Category (I’m assuming that’s a domain type and not a type generated by an ORM.
then set the dependency in the ViewModel to this ICategoryRepository;
Then act on this dependency from your ViewModel, your ViewModel SHOULD NOT be calling a database directly which is what you seem to be implying.
your code:
should reside in the GetAll() of the repository. Move it out of the ViewModel.
your setting of the observable collection should be done in the ctr:
to this: