从集合 ObservableCollection 更新数据库

发布于 2025-01-05 15:14:10 字数 676 浏览 6 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-01-12 15:14:10

正确的方法是在存储库模式后面有一个数据库访问层:

public interface IRepository<T>
{
   IEnumerable<T> GetAll();
   T GetById(int id);
   void Save(T saveThis);
   void Delete(T deleteThis);
}

然后使用您的域类型类别来实现它(我假设这是一个域类型,而不是由 ORM 生成的类型。

public interface ICategoryRepository : IRepository<Category>
{
    // add any methods that are needed to act on this specific repo
}

然后将 ViewModel 中的依赖项设置为this ICategoryRepository;

private readonly ICategoryRepository _categoryRepo;

public ViewModel(ICategoryRepository categoryRepo)
{
    _categoryRepo = categoryRepo;
}

然后根据您的 ViewModel 的依赖关系采取行动,您的 ViewModel 不应直接调用数据库,这似乎是您

的代码所暗示的:

sdb.ExecuteSprocAccessor <Category> ("Get_Categories_List");

应驻留在存储库的 GetAll() 中。 ViewModel 的

可观察集合的设置应该在 ctr: 中完成

ListCategories = categories.ToObservableCollection <Category>();

public ViewModel(ICategoryRepository categoryRepo)
{
    _categoryRepo = categoryRepo;
    var categories = _categoryRepo.GetAll();
    ListCategories = categories.ToObservableCollection <Category>();
}

The proper way is to have a DB Access layer behind the Repository pattern of:

public interface IRepository<T>
{
   IEnumerable<T> GetAll();
   T GetById(int id);
   void Save(T saveThis);
   void Delete(T deleteThis);
}

then implement this with your domain type Category (I'm assuming that's a domain type and not a type generated by an ORM.

public interface ICategoryRepository : IRepository<Category>
{
    // add any methods that are needed to act on this specific repo
}

then set the dependency in the ViewModel to this ICategoryRepository;

private readonly ICategoryRepository _categoryRepo;

public ViewModel(ICategoryRepository categoryRepo)
{
    _categoryRepo = categoryRepo;
}

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:

sdb.ExecuteSprocAccessor <Category> ("Get_Categories_List");

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:

ListCategories = categories.ToObservableCollection <Category>();

to this:

public ViewModel(ICategoryRepository categoryRepo)
{
    _categoryRepo = categoryRepo;
    var categories = _categoryRepo.GetAll();
    ListCategories = categories.ToObservableCollection <Category>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文