[Translation] Set in WCF Ria Services (1)

Source: Internet
Author: User
Tags asin

Address: http://www.silverlightshow.net/items/Working-with-collections-in-WCF-RIA-Services-part-one.aspx

This article is divided into two parts. This is the first part.

Introduction

Today, many commercial applications are made up of WCF Ria services (not surprisingly, it is indeed a powerful high-scalability framework ). However, it can better support the collection type. You often perform this operation: Get an object (usually a load operation) and add it to an observablecollection in its completion event. You can still do this now. However, in the first SP of WCF Ria services, the existing collection types are enhanced, and some new collection types are added by colleagues. These changes make you more comfortable when using WCF Ria services in combination with the mvvm mode. Now, our available collection can automatically track your domaincontext. You can add filtering, sorting, and even grouping conditions, and provide a server-side paginated domaincollectionview. In this articleArticle, We will discuss these enhancements and new collection types together, and see which scenarios they are used more effectively.

This article will cover four Collection types in WCF Ria services: entityset (enhancement), entitylist (new), collectionview (enhancement), and domaincollectionview (new ). You can download the source code here.

WCF Ria Services SP1 is included in Visual Studio 2010 SP1. Click here to download it.

Entityset <t>

Entityset is a WCF Ria services application.ProgramA basic set type in-it is often used when we create a viewmodel. It is an unordered set with some options for customizing the return type. Therefore, when we want to view all objects of a certain type Loaded through domaincontext, we often use it.

Is a sample program. I bind the books attribute to ListBox.

See the followingCode:

 
/// <Summary> ///The books Property/// </Summary>PublicEntityset<Book> Books {Get{ReturnContext. Books ;}}

As you can see, the books attribute is a collection of book-type entitysets, which is just a simple reference of context. Books. In the constructor, we load 10 data records:

 
PublicEntitysetviewmodel () {instantiatecommands ();// Load booksContext. Load <Book> (Context. getbooksquery (). Take (10));}

When more data is needed

 
Loadmorebooks =NewRelaycommand() => {Context. Load <Book> (Context. getbooksquery ());});

When more book-type entities are added to the correct context. Books entityset, these newly added books are also loaded into ListBox. In other words, when the book data is loaded by domaincontext, they are also added to the entityset contextbooks.

Entityset <t>:Add and remove data

So how do I add or remove data to or from entityset? The following code adds data:

 
Addbook =NewRelaycommand() => {Context. Books. Add (NewBook() {Author ="Kevin dockx", Asin ="123456", Title ="Dummy book"});});

Then remove the data:

Deletebook =NewRelaycommand() => {Context. Books. Remove (context. Books. firstordefault ());});

When you call the submitchanges () method, the domainservice will reflect the added/deleted data as the corresponding operations of the server.

Enitylist <t>

The next collection type to be introduced is entitylist, which is a new collection type. You can find it in the WCF Ria services toolkit, which is located in Microsoft. windows. data. under the domainservices namespace (the source code already contains this assembly ). Essentially, it is an object set based on entityset. It allows us to obtain a subset of a specific type of object data loaded through domaincontext. Define an entitylist <t> attribute as follows:

 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 has a source attribute that defines the entities that entitylist should contain:

PublicEntitylistviewmodel () {instantiatecommands ();// Load booksThis. Books. Source = context. Load <Book> (Context. getbooksquery (). Take (10). Entities ;}

With these two pieces of code, your entitylist has been initialized and ready.

Once you start to read more book-type entities, things become interesting: data changes will not be automatically reflected in your entitylist. If you want entitylist to respond to the newly added books, you need to set its source attribute:

Loadmorebooks =NewRelaycommand() => {This. Books. Source = context. Load <Book> (Context. getbooksquery (). Entities ;});

Entitylist <t>:Add and remove data

To add a new book object, you can directly add it to context or to entitylist. The two methods need to be processed in different ways. We can see the code in our example:

Addbook =NewRelaycommand() => {Context. Books. Add (NewBook() {Author ="Kevin dockx", Asin ="123456", Title ="Dummy book"});});

A new book is added to the context. However, entitylist does not automatically learn these changes (as mentioned above, to track changes in the subset of items, you need to set the source attribute instead of loading changes in each context ), the ListBox still displays the Entity Data Set in the source attribute of entitylist.

When the following code is executed:

Addbooktoentitylist =NewRelaycommand() => {Books. Add (NewBook() {Author ="Kevin dockx", Asin ="123456", Title ="Dummy book"});});

A new book object is added to the entitylist, which is directly reflected in our ListBox control. If this book entity does not exist in the entityset, it will be added to its corresponding entityset at the same time.

The act of removing a book is different: When you remove the book data from the entityset of context, it will be directly reflected in the entitylist:

Deletebook =NewRelaycommand() => {Context. Books. Remove (context. Books. firstordefault ());});

When you remove a book from the entitylist, the entity is also removed from the entityset.

 
Deletebookfromentitylist =NewRelaycommand() =>{ Books. Remove (books. firstordefault ());});

This is the first part of this article. The second part will introduce more advanced Collection types, including icollectionview and domaincollectionview.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.