WPF、WCF、Entity、MVVM疑惑!

发布于 2024-10-08 05:14:14 字数 512 浏览 3 评论 0原文

我在 WPF 项目中使用 WCF 服务引用,并且我的实体框架数据模型驻留在 WCF 项目中。

我正在使用 MVVM Light 框架。我正在做以下事情:

  1. 我在服务中使用 LINQ 来获取数据,然后从 WPF(通常是 obersvablecollections)中获取数据。
  2. 一切都在视图部分中进行,例如根据需要填充数据网格和视图。

但我有以下疑问:

  1. 这是在 wcf 和 wpf 之间传输数据的正确方法吗?
  2. 我还没有使用该模型做任何事情,我不确定何时使用它?
  3. 我还想保存数据网格中的数据。我能够将数据网格更新数据的可观察集合传递给服务的函数。但是我如何更新这个集合中的实体呢?通过循环?听起来不对。一旦我从该集合中更新实体,我将能够使用 saveChanges 更新到数据库中。
  4. 当我需要在树视图中显示分层数据时,在哪里从存储过程 xml 使该数据分层?使用视图创建分组条件列?在服务中创建此列?在演示文稿中创建此列/属性?

I am using a WCF service reference in a WPF project, and my entity framework data model resides in WCF project.

And I am using MVVM Light framework. I am doing the following things:

  1. I use LINQ in the service to get data and then fetch it from WPF, obersvablecollections usually.
  2. Everything works in view part like populating datagrid, views as required.

But I have following doubts:

  1. Is this correct way of transferring data between wcf and wpf.
  2. I haven't used the Model for anything yet, I have doubt about when to use it?
  3. I also wanted to save data from datagrid. I was able to pass on the observablecollection of updated data of datagrid to the service's function. But how do i update the entity from this collection? by looping? doesnt sound right. Once I update the entity from this collection I will be able to use saveChanges to update into database.
  4. When I need to show hierarchal data in a treeview, where to make that data hierarichal, from stored procedure xml? use a view to create a grouping criteria column? create this column in service? create this column/property in presentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最美不过初阳 2024-10-15 05:14:14

1 - 没有正确的方法,这取决于您的要求和目标。

2 - 使用 MVVM,模型应位于 WPF 和数据库之间。这意味着对数据库的所有调用都应该通过该模型,并且对数据库的所有写入也应该通过该模型。 WPF GUI 应该只绑定到模型。这通常意味着您的 WPF 部分主要由 XAML 代码组成。所有访问数据库的代码都应该位于模型中。

将其分开是有充分理由的。

  • 您可以在模型上编写单元测试。
  • 视图模型独立于 GUI 的外观。这意味着您可以通过放入不同的组件并仅绑定到模型来轻松更改 GUI。
  • 快速谷歌搜索可能会产生更多原因。

3 - 我会尝试仅发送已更改的实体。这可以通过将集合传递到视图模型并让视图模型找出发生了什么变化来完成。

4 - 我不太明白你想做什么。通常,要创建 TreeView,您应该为每个视图模型创建 HierarchicalDataTemplate。 TreeView 控件将处理其余的事情。你真的应该做一些关于这个的教程,因为它有点难以理解。

1 - There is no correct way, it depends on your requirements and goals.

2 - With MVVM, the model should sit between WPF and the database. That means all calls to the database should go through the model, and all writes to the database should also go through the model. The WPF GUI should only bind to the model. This usually means that your WPF portion consists mostly of XAML code. All code that accesses the database should be in the model.

There are good reasons for separating this.

  • You can write unit tests that on the model.
  • The view model is independent from the look of the GUI. This means that you can easily change the GUI by dropping in different components and just binding to the model.
  • A quick google search can probably yield more reasons.

3 - I would try to send over only the entities that have changed. This can be done by passing the collection to your view model, and have your view model figure out what has changed.

4 - I don't quite understand what you want to do. Usually, to make a TreeView, you should create HierarchicalDataTemplate for each of your view models. The TreeView control will take care of the rest. You should really do some tutorials on this one, because it's kinda hard to wrap your head around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文