在 Silverlight 3.0 中结合 .NET RIA 服务和 MVVM

发布于 2024-07-16 20:07:31 字数 226 浏览 2 评论 0原文

在 Silverlight 3.0 中使用 .NET RIA 服务和 MVVM 时,RIA 服务中的元数据类型与 MVVM 模式中的 ViewModel 之间有区别吗? 这些是同一件事还是应该分开?

元数据类型是部分实体类的密封内部类。 那里似乎没有适当的分离,但元数据类型也可以用验证属性来装饰,这使得它看起来像一个 ViewModel。

我四处搜寻,但没有看到任何详细讨论此问题的内容。

When using .NET RIA Services and MVVM in Silverlight 3.0 is there a difference between the Metadata type from RIA Services and the ViewModel from the MVVM pattern? Are these the same thing or should they be keep separate?

The metadata type is a sealed internal class to the partial Entity class. There doesn't seem to be a proper separation there but the metadata type can also be decorated with attributes for Validation which makes it look like a ViewModel.

I've searched around but I didn't see anything that talks about this in any detail.

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

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

发布评论

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

评论(2

贱人配狗天长地久 2024-07-23 20:07:31

同意 ChuckJ - 通常 DomainContext 构成视图模型的一部分。 例如,假设我有一个搜索页面,允许搜索产品目录。 以下是我的结构方式:

在服务器上:

class Catalog : DomainService {
    IQueryable<Product> GetProducts(string keyword) { ... }
}

生成的 DomainContext:

class Catalog : DomainContext {
    EntityList<Product> Products { get; }
    void LoadProducts(string keyword);
}

我要编写的视图模型:

class SearchViewModel {
    Catalog _catalog = new Catalog();

    public IEnumerable<Product> Results {
        get { return _catalog.Products; }
    }

    public void Search(string keyword) {
        _catalog.Products.Clear();
        _catalog.LoadProducts(keyword);
    }
}

最后在我的 xaml 中,我将 UserControl 的 DataContext 设置为 SearchViewModel 的实例,并将 ItemsControl 绑定到结果属性。 我将使用您选择的 ViewModel 模式将按钮单击绑定到搜索(这实际上是 SearchViewModel 公开的命令)。 我个人喜欢与 Silverlight.FX 合作的东西,如下所示:

<Button Content="Search"
  fxui:Interaction.ClickAction="$model.Search(keywordTextBox.Text)" />

以及最初显示的此处

正如 Chuck 提到的,我的视图模型中可能确实有其他状态,例如,SelectedProduct 可能双向绑定到 xaml 中 ListBox 的 SelectedItem,然后将相同的 SelectedProduct 绑定为 DataForm 的 DataContext 以显示所选产品的详细信息。

希望有帮助! 我很快就会在我的博客上写更多有关此内容的博客。

Agree with ChuckJ - generally the DomainContext forms part of a view model. For example, say I had a search page that allowed searching against a product catalog. Here is how I'd structure things:

On the server:

class Catalog : DomainService {
    IQueryable<Product> GetProducts(string keyword) { ... }
}

The generated DomainContext:

class Catalog : DomainContext {
    EntityList<Product> Products { get; }
    void LoadProducts(string keyword);
}

The view model I would write:

class SearchViewModel {
    Catalog _catalog = new Catalog();

    public IEnumerable<Product> Results {
        get { return _catalog.Products; }
    }

    public void Search(string keyword) {
        _catalog.Products.Clear();
        _catalog.LoadProducts(keyword);
    }
}

And then finally in my xaml, I'd set my UserControl's DataContext to be an instance of SearchViewModel, and bind an ItemsControl to the Results property. I'd use the ViewModel pattern of your choice to bind a button click to Search (which is effectively a command that SearchViewModel exposes). I personally like something that I have working with Silverlight.FX as in:

<Button Content="Search"
  fxui:Interaction.ClickAction="$model.Search(keywordTextBox.Text)" />

and as initially shown here.

As Chuck mentions I might indeed have other state in my view model, for example, the SelectedProduct that might be two-way bound to the SelectedItem of a ListBox in my xaml, and then bind the same SelectedProduct as the DataContext of a DataForm to show details of a selected product.

Hope that helps! I'll be blogging about this some more on my blog soon.

流殇 2024-07-23 20:07:31

RIA 服务数据上下文被设计为在 MVVM 模式中扮演 ViewModel 的角色,因为它们本身支持数据绑定,但它们在文档中不使用该术语。 然而,这确实取决于。 您可能需要视图模型中的状态,而不是 RIA 数据上下文提供的状态,例如命令和其他与视图相关的状态。 我认为您想要做的是将 RIA 服务中的数据上下文用作视图模型的一部分。

The RIA services data context was designed to play the role of a ViewModel in the MVVM pattern since they natively support data binding, but they don't use that term in their documentation. However, it really depends. You will probably need state in your view model than the RIA data context provides such as commands and other view related state. I think what you want to do is use the data contexts from the RIA services as a part of the view model.

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