SpriteHand
Module Border
  Using RIA Services Programmatically
Module Border
Location: BlogsAndy's Blog    
Posted by: host 1/7/2010 9:56 AM


Many demos of WCF RIA Services show off using the DomainDataSource Class to in a declarative manner to quickly throw together a data-enabled application. And in VS2010, there is added support for using the Data Sources Window to Drag/Drop data entities onto the designer --- this action also adds a DomainDataSource instance into your XAML.

For many applications, using a DomainDataSource in XAML is fine and enables RAD development. But some pundits (Hi Shawn!) argue that having direct interaction with the Data Layer from the UI Layer is bad for proper Model/View separation, testability, and general karma.

Luckily, there are several ways to use WCF RIA Services programmatically - without relying on the DomainDataSource controls in XAML. Let's take a look at some of these programmatic methods:

Download the Demo Code
  * requires SL4/VS2010 Beta 2, WCF RIA Services Beta

Using the DomainContext

The DomainContext class allows the Silverlight client to access the Domain Service Class you created on the server side. A new DomainContext class is automatically generated on the client side for you when you create a new Domain Service.

In the demo, our DomainContext class is named BeerContext. We can load all of the Beer rows from the database using the Load method, passing in an entity query that exists in our Domain Service:

BeerContext _beerContext = new BeerContext();


LoadOperation<Beer> loadBeers = _beerContext.Load(_beerContext.GetBeersQuery());

grdLoadFromContext.ItemsSource = loadBeers.Entities;

We can also use a LINQ query against a DomainContext, allowing us to request more specific records:

EntityQuery<Beer> beerQuery = from b in _beerContext.GetBeersQuery()

                               where b.BrewerId == 1

                               select b;

LoadOperation<Beer> loBeers = _beerContext.Load(beerQuery);

grdLoadWithLinq.ItemsSource = loBeers.Entities;

Using DomainDataSource In Code

Normally, you will see the DomainDataSource class used declaratively in XAML. But we can also utilize a DDS through code and take advantage of its paging, filtering, sorting and grouping functions . Consider this code, which creates a DDS and executes the GetBeers query, only retreiving the Beers for BrewerId == 1. Note that only the records for BrewerId == 1 are sent from the server, because the DDS creates an appropriate query for any paging, filtering, sorting, or grouping functions.

DomainDataSource dsBeers = new DomainDataSource();

dsBeers.LoadSize = 30;

dsBeers.QueryName = "GetBeers";

dsBeers.AutoLoad = true;

dsBeers.DomainContext = _beerContext;

dsBeers.SortDescriptors.Add(new SortDescriptor("BeerName", System.ComponentModel.ListSortDirection.Ascending));

dsBeers.FilterDescriptors = new System.Windows.Data.FilterDescriptorCollection();

dsBeers.FilterDescriptors.Add(new System.Windows.Data.FilterDescriptor("BrewerId", System.Windows.Data.FilterOperator.IsEqualTo, 1));

grdLoadDomainDataSource.ItemsSource = dsBeers.Data;
dsBeers.Load();

 

Permalink |  Trackback

Comments (9)   Add Comment
Re: Using RIA Services Programmatically    By Anonymous on 1/7/2010 3:12 PM
Quick answer... Shawn is right IMO. Keep it out of the XAML if you can.

Re: Using RIA Services Programmatically    By Anonymous on 1/8/2010 9:23 AM
Now if I can get the Pager working in an MVVM context, I'll be finally be happy :P

Re: Using RIA Services Programmatically    By Anonymous on 2/25/2010 12:27 PM
I tried the same approach in my application of using DomainDataSource class from my ViewModel. Whenever I set LoadSize property my Load method fetches record first time correctly. Immediately i see the second call shooting up and ending with exception. I am not able to get the exception in the overriden OnError method at the Domainservice end also.. Am i missing something there ???

Re: Using RIA Services Programmatically    By Anonymous on 3/5/2010 8:54 AM
in order for loadsize to work, I have found that you need to set an orderby

Re: Using RIA Services Programmatically    By Anonymous on 3/17/2010 8:29 PM
Thank you so much. You saved the day!

Re: Using RIA Services Programmatically    By Anonymous on 6/15/2010 6:22 AM
Well done, it's good to see solid useful ways of using DomainDataSources outside the View.

Re: Using RIA Services Programmatically    By Anonymous on 11/17/2010 10:15 AM
this is beautiful. Thank you.

Re: Using RIA Services Programmatically    By Anonymous on 1/11/2011 5:49 PM
I download the project but it fails to build as it can't find the namespace for: BeerContext

Re: Using RIA Services Programmatically    By Anonymous on 5/16/2011 11:06 PM
Nice work dude...this helped thanks mate..


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2012 andy.beaulieu.com - Login