惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
罗磊的独立博客
F
Fortinet All Blogs
T
Threatpost
Y
Y Combinator Blog
博客园_首页
美团技术团队
Security Latest
Security Latest
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
V
V2EX - 技术
The Cloudflare Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
Jina AI
Jina AI
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Hacker News
The Hacker News
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Latest news
Latest news
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
雷峰网
雷峰网
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
W
WeLiveSecurity
D
DataBreaches.Net
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
S
Securelist
Help Net Security
Help Net Security

博客园 - 紫色永恒

一段旅程的结束和另一端旅程的开始 Silverlight Client←→Server数据同步备忘代码 Expression Blend 5 Preview For Silverlight5 RC已发布 Siverlight5新功能/改进总结 [译]WCF RIA Services中的集合(2) Silverlight通过MVVM实现多语言实时切换(含源代码) Silverlight中服务通信方式的选择(WCF、Data Service、Ria Service) 你应该知道的,那些未在Silverlight5Beta中出现的特性 记录来敦煌一周的情况 Silverlight奇技银巧7 - 巧用文件生成时间强制客户端载入最新xap文件 [ASP.NET MVC3]Chart的ActionResult扩展 如何将ASP.NET MVC2项目升级到MVC 3 RC Silverlight 4 GDR 1 更新 Silverlight奇技银巧6 - UserControl的继承 Silverlight 4 Training Kit更新 - 紫色永恒 Silverlight4的DataGrid改进与Toolkit2010.4中的新玩意 - 紫色永恒 Win03+IIS6部署.NetFramework4(ASP.NET4)的一点小经验 - 紫色永恒 4.13 Silverlight4 Will Come - 紫色永恒 Silverlight4RC的API汇总(文本文件) - 紫色永恒
[译]WCF RIA Services中的集合(1)
紫色永恒 · 2011-07-15 · via 博客园 - 紫色永恒

原文地址:http://www.silverlightshow.net/items/Working-with-collections-in-WCF-RIA-Services-part-one.aspx

本文分为两部分,本篇为第一部分。

介绍

今天,很多的商业应用是使用WCF RIA Services构成的(这并不奇怪,它确实是一个强大的高扩展性框架)。然而它对集合类型的支持可以变得更好。你经常会做这样的操作:获取实体(通常是一个Load Operation)在其完成事件中将其添加到一个ObservableCollection<T>中,现在你依然可以这样做。不过在WCF RIA Services的第一个SP中分别对一些之前就存在的集合类型进行了加强,同事也增加了一些新的集合类型。这些变化使你在使用WCF RIA Services配合MVVM模式时更得心应手。现在我们可用的集合可以自动跟踪你的DomainContext,你可以添加过滤、排序甚至分组条件,还提供了一个服务端可分页的DomainCollectionView。在这篇文章中,我们会一起探讨一下这些增强及新增的新集合类型以及看看它们在哪些场景中使用起来更给力。

本文中将会涉及WCF RIA Services中的四种集合类型:EntitySet(增强)、EntityList(新增)、CollectionView(增强)以及DomainCollectionView(新增)。你可以在这里下载源码

WCF RIA Services SP1包含在Visual Studio 2010 SP1中,点此下载

EntitySet<T>

EntitySet是WCF RIA Services应用程序中的一个基本集合类型—当我们建立ViewModel时会经常用到它。它是一个带有一些用来自定义返回类型选项的无序集合。因此当我们想查看通过DomainContext载入的某个类型的所有实体时,我们会经常使用它。

下图是一个示例程序,我将Books属性绑定到ListBox上。

clip_image002

看下面的代码:

/// <summary>
/// The Books property
/// </summary>
public EntitySet<Book> Books{
    get{
        return Context.Books;
    }
}

像你看到的那样,Books属性是一个Book类型的EntitySet集合,它只是Context.Books的简单引用。在构造函数中,我们载入10条数据:

public EntitySetViewModel() {

    InstantiateCommands();

    // load books
    Context.Load<Book>(Context.GetBooksQuery().Take(10));
}

当需要更多数据时,这样

LoadMoreBooks = new RelayCommand(() => {
    Context.Load<Book>(Context.GetBooksQuery());
});

当更多的Book类型的实体被添加到正确的Context.Books这个EntitySet时,这些新增的Books也被加载到ListBox中。换句话说,当Book数据被DomainContext载入时,它们也被同时添加到ContextBooks这个EntitySet中了。

EntitySet<T>:添加和移除数据

那么如何向EntitySet添加或移除数据呢?下面的代码展示如何添加数据:

AddBook = new RelayCommand(() => {
    Context.Books.Add(new Book() {
        Author = "Kevin Dockx"
        ,
        ASIN = "123456"
        ,
        Title = "Dummy book"
    });
});

然后是移除数据:

DeleteBook = new RelayCommand(() => {
    Context.Books.Remove(Context.Books.FirstOrDefault());
});

当你调用SubmitChanges()方法时,DomainService会将增加/删除的数据切实的反映为服务端的相应操作。

EnityList<T>

下一个要介绍的集合类型是:EntityList<T>,它是一个新增的集合类型,你可以在WCF RIA Services Toolkit中找到它,它位于Microsoft.Windows.Data.DomainServices命名空间下(源码中已经包含了这个程序集)。本质上说它是一个基于EntitySet的Observable集合。它的优点是允许我们取得通过DomainContext载入的特定类型实体数据的子集。定义一个EntityList<T>属性大概是如下的样子:

private EntityList<Book> _books;
public EntityList<Book> Books {
    get {
        if (this._books == null) {
            this._books = new EntityList<Book>(
                this.Context.Books);
        }
        return this._books;
    }
}

Entity List有一个Source属性,它定义了EntityList应该包含的实体:

public EntityListViewModel() {
    InstantiateCommands();

    // load books
    this.Books.Source = Context.Load<Book>(Context.GetBooksQuery().Take(10)).Entities;
}

通过这两段代码,你的EntityList就已经初始化并准备好了。

一旦你开始读取更多的Book类型实体,事情就变得有趣起来了:数据变化不会自动的反映到你的EntityList中。如果你想让EntityList对那些新增的Book作出反应,则需要设置它的Source属性:

LoadMoreBooks = new RelayCommand(() => {
    this.Books.Source = Context.Load<Book>(Context.GetBooksQuery()).Entities;
});

EntityList<T>:添加和移除数据

添加一条新的Book实体通常通过两种方式,直接添加到Context中或添加到EntityList本身。这两种方式需要使用不同的方法处理。可以看到我们示例中的代码:

AddBook = new RelayCommand(() => {
    Context.Books.Add(new Book() {
        Author = "Kevin Dockx"
        ,
        ASIN = "123456"
        ,
        Title = "Dummy book"
    });
});

一条新的Book被添加到Context中。不过EntityList不会自动的获知这些变化(上文说过,实现项的子集的变化跟踪需要通过设置Source属性,而非每次Context的载入变化),ListBox仍然显示EntityList的Source属性中的实体数据集合。

当如下代码执行时:

AddBookToEntityList = new RelayCommand(() => {
    Books.Add(new Book() {
        Author = "Kevin Dockx"
        ,
        ASIN = "123456"
        ,
        Title = "Dummy book"
    });
});

一条新的Book实体被添加到EntityList中,这将直接反映到我们的ListBox控件中。如果EntitySet中不存在这条Book实体,则它会被同时添加到其对应的EntitySet中。

而移除一条Book的行为又有一些不同:当你从Context的EntitySet中移除Book数据的时候,它会直接反映到EntityList中:

DeleteBook = new RelayCommand(() => {
    Context.Books.Remove(Context.Books.FirstOrDefault());
});

当你从EntityList中移除一条Book时,EntitySet中也会同时移除这条实体。

DeleteBookFromEntityList = new RelayCommand(() => {
    Books.Remove(Books.FirstOrDefault());
});

这是本文的第一部分,第二部分将会对更高级的集合类型进行介绍,他们是:ICollectionView和DomainCollectionView,敬请期待。