Silverlight 4 WCF RIA 服务和 MVVM 没那么简单

发布于 2024-08-30 07:15:26 字数 901 浏览 4 评论 0原文

[免责声明:我是 ASP.NET MVC 开发人员]

嗨,

我正在寻找一些在 Silverlight 4 中使用 WCF RIA 实现 MVVM 模式的最佳实践。

我不打算使用 IoC 的 MEF用于定位我的 ViewModel。我想知道的是如何将MVVM模式与Silverlight 4和WCF RIA一起应用。

我不想使用 Prism 或 MVVM Light 工具包等其他东西。我在 Internet 上找到了许多示例,展示了将数据源拖放到视图上并完成工作是多么美妙(这让我想起了我的第一个 VB6 开发)。

我尝试使用 WCF RIA 实现 MVVM,但它一点也不简单。如果我理解的话,MVVM 应该包含所有逻辑,以便对其进行单独的单元测试,但是当将其与 WCF RIA 结合起来时,那就是另一回事了。我有以下问题。

  1. 我可以使用生成的元数据作为模型吗?如果我从头开始编写所有内容,那么使用它会更容易。

  2. 正如我所见,获取数据的唯一方法是通过 DomainContext 或通过视图(本地资源)中的直接绑定。我不想在视图中直接绑定,根本不可测试。另一方面,我无法使用 DomainContext,它不会公开任何单个实体!我所拥有的只是可以绑定到数据网格的 EntitySet。如何从 ViewModel 将单个实体绑定到 DataForm?

  3. 如何将模型更新到数据库?

  4. 如何从一个实体导航到其项目集合。例如,如果我有一个公司实体,我想显示一个数据表单来更新实体信息,并显示一个数据网格来显示公司地址。保存表单时,我想将信息保存到公司,并将信息保存到地址,以了解哪个地址被选为活动地址。

请帮助我了解如何做好它。或者也许我应该放弃 WCF RIA 并从头开始使用 WCF 来完成它?

你怎么认为 ?

[Disclaimer: I'm ASP.NET MVC Developer]

Hi,

I'm looking for some best practices with implementing MVVM pattern with WCF RIA in Silverlight 4.

I'm not looking to use MEF of IoC for locating my ViewModels. What I would like to know is how to apply MVVM pattern with Silverlight 4 and WCF RIA.

I don't want to use other stuff like Prism or MVVM Light toolkit. I found many examples on Internet showing how it is wonderful to drag and drop a datasource on the view and the job is done (it reminds me about my first VB6 developments).

I tried to implement MVVM with WCF RIA and it's not strightforward at all. If I understand, the MVVM should contain all the logic in order to unit test it in isolation but when it comes to combine it with WCF RIA it's another story. I have the following questions.

  1. Can I use a generated metadata as model ? It would be easier to use it that if I write all from the scratch.

  2. As I saw the only way I could get data is through DomainContext or through direct binding in the view (local ressource). I don't want the direct binding in the view, not testable at all. On the other hand I can't use DomainContext, it doesn't expose any single entity !!! All I have is the EntitySet that I can bind to datagrid. How do I bind a single Entity to the DataForm from the ViewModel ?

  3. How do I udpate the model to the database ?

  4. How do I navigate from one Entity to a collection of it's items. For example if I have a Company Entity I would like to show a DataForm to update an entity informations and a datagrid to show companies adresses. When saving a form, I would like to save an information to Company and an information to adress about which adress was selected as active.

Please help me understand how to do it well. Or maybe I should drop the WCF RIA and to do it with WCF from scratch ?

What do you think ?

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

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

发布评论

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

评论(4

要走就滚别墨迹 2024-09-06 07:15:27

您可能对此会话感兴趣。它解释了如何将 MVVM 模式与 RIA WCF 服务结合使用。

You might be interested in this session. It explains how to use the MVVM pattern with RIA WCF Services.

情独悲 2024-09-06 07:15:27

一些随机的答案...

我不认为 MEF 特别适合 Silverlight。它主要用于桌面应用程序,并且可以适用于插件位于与应用程序相关的本地文件系统中的其他用途。

MVVM 要求您了解角色。 “视图”是您的 XAML 和代码隐藏。代码隐藏应该处理来自用户控件的事件,但仅此而已。

ViewModel 保存用户控件将绑定到的数据。通常,ViewModel 作为其 DataContext 绑定到 View,以便表单中的所有内容都可以数据绑定到 ViewModel 中的属性。 ViewModel 必须实现 INotifyPropertyChanged,并为表单数据绑定到的每个属性引发属性更改事件。

您可能希望使用 EntitySet 作为源来创建 ObservableCollection。这将出于数据绑定目的处理 INotifyCollectionChanged。如果 EntitySet 中的实体也处理 INotifyPropertyChanged,那么您在集合数据绑定方面处于良好状态。

您可以为单个实体创建一个属性,并对其进行数据绑定,假设还实现了更改通知(对于实体成员和实体属性)。

RIA 服务将在每次构建时重新生成 DomainContext,这有助于保持同步。不过,它旨在成为 ORM 之上的服务层,因此您的 ORM 或其他数据映射仍然需要通过其他方式进行维护。

我还没有看过 RIA Services 的最终版本,但测试版并没有给我留下深刻的印象。我宁愿在服务器上定义好的实体类,并与 Silverlight 项目共享它们。不过,它的设置并不容易,并且需要一些不依赖于服务引用的重要 WCF。 (RIA Services 最终版本可能已经清理了其中的一些内容,但是 Silverlight 中的本机 WCF 服务引用非常邪恶,主要是因为它不会自动重新创建生成的类,并且它对服务器端服务的 URI 进行了硬编码.)

元数据是 RIA Services beta 的另一个问题。如果您控制实体源,则可以更轻松地将元数据属性直接附加到 DataContract 类和各个 DataMember 属性。同样,这可能意味着不使用 RIA 服务。按照 RIA 测试版的要求编写单独的元数据类并不是一个好的解决方案。

我最终没有使用 Silverlight 3 的 RIA 服务,并且并不后悔。这是有关 WCF 和 Silverlight 的优秀文章 。尽管它说是 Silverlight 2,但它仍然是任何 Silverlight 版本的目标。

我推荐 MVVM Light。如果有问题,可以在 Codeplex 上找到源代码。它提供消息传递和命令支持,以及 ViewModelLocator;虽然后者需要一些工作才能理解,但它确实是基本 MVVM 模型的一个很好的扩展。

希望这有帮助......

Some random answers...

I don't think that MEF is particuarly well suited for Silverlight. It's primarily for desktop apps, and could be adapted for other uses where the plug-ins are in the local file system relative to the app.

MVVM requires that you understand roles. The "view" is your XAML and code-behind. The code-behind should handle events from the user control, but very little more than that.

The ViewModel holds the data that the user control will bind to. Generally, the ViewModel is bound to the View as its DataContext, so that everything in the form can databind to properties in the ViewModel. The ViewModel must implement INotifyPropertyChanged, and raise property changed events for every property that the form databinds to.

You'll probably want to create an ObservableCollection, using an EntitySet as your source. This will handle INotifyCollectionChanged for databinding purposes. If the entities in the EntitySet also handle INotifyPropertyChanged, then you're in good shape on databinding for collections.

You can create a property for an individual entity, and databind to that, assuming that change notification is also implemented (both for entity members, and for the entity property).

RIA Services will regenerate the DomainContext on each build, which helps a little in keeping it in sync. It's intended to be a service layer above an ORM, though, so your ORM or other data mapping will still have to be maintained by other means.

I haven't looked at the final release of RIA Services, but I wasn't hugely impressed with the beta version. I'd rather have good entity classes defined on the server, and share them with the Silverlight project. It's not easy to set up, though, and requires some non-trivial WCF that doesn't rely on service referernces. (RIA Services final release may have cleaned some of this up, but the native WCF service reference in Silverlight is pretty much evil, mainly because it doesn't automatically recreate generated classes, and it hard-codes the URI for the server-side service.)

Metadata was another problem with RIA Services beta. It's easier to attach metadata attributes directly to your DataContract class and the individual DataMember properties, if you control the entity source. Again, that may mean not using RIA Services. Writing a separate metadata class, as was required for the RIA beta, wasn't a good solution.

I ended up not using RIA Services for Silverlight 3, and didn't regret it. Here's an excellent article on WCF and Silverlight. Although it says Silverlight 2, it's still on-target for any Silverlight release.

I do recommend MVVM Light. Source is available on Codeplex, if that's an issue. It provides messaging and commanding support, as well as a ViewModelLocator; while the latter takes a bit of work to understand, it's really a good extension to the basic MVVM model.

Hope this helps.....

-柠檬树下少年和吉他 2024-09-06 07:15:27

只是想让你知道我正在从事的一个项目 - 刚刚完成了我们的第一个版本。提供了一种非常简单的方法来专门针对 Silverlight + RIA 服务处理 MVVM。简化了许多 MVVM 的内容,并通过通知类提供了一些更多类似控制器的功能。 http://slmvvms.codeplex.com/

Just thought I would let you know about a project I am working on - just got our first release done. Provides a great simple way to approach MVVM for Silverlight + RIA Services specifically. Simplifies a lot of the MVVM stuff, and provides some more controller-like functions with the Notifications class. http://slmvvms.codeplex.com/

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