从集合 ObservableCollection 更新数据库
我目前正在使用 EnterpriseLibrary 5.0 和 MVVM:
我有一个绑定到可编辑组合框的 ObservableCollection ListCategories
属性(我可以添加/删除/编辑类别):
我有以下代码:
public ObservableCollection<Category> ListCategories
{
get
{
return listCategories;
}
set
{
listCategories = value;
}
}
var categories = sdb.ExecuteSprocAccessor <Category> ("Get_Categories_List");
ListCategories = categories.ToObservableCollection <Category>();
我的问题:
集合中发生所有更改后,如何更新回数据库?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是在存储库模式后面有一个数据库访问层:
然后使用您的域类型类别来实现它(我假设这是一个域类型,而不是由 ORM 生成的类型。
然后将 ViewModel 中的依赖项设置为this ICategoryRepository;
然后根据您的 ViewModel 的依赖关系采取行动,您的 ViewModel 不应直接调用数据库,这似乎是您
的代码所暗示的:
应驻留在存储库的 GetAll() 中。 ViewModel 的
可观察集合的设置应该在 ctr: 中完成
:
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: