Logo Subtitle Subtitle

How to retrieve many to many (N-N) data from WCF RIA services

Download Demo project ManyToMany.zip

This tutorial shows how to retrieve Many-to-Many data in a Silverlight RIA Application. Here is our data model:
 

I want to show a list of movies and each movie contains actors:
 

Set up your project

(or download Demo project ManyToMany.zip)

The following things are pretty common for a typical Silverlight Business Application:

  • Create a Silverlight Application with RIA services enabled
  • Create a database
  • Create an ADO.NET Entity model from database
  • -=*> BUILD THE PROJECT <*=–
  • Create a DomainService from the entity model
  • Create a ViewModel
  • Create a UI that holds a Listbox for the movies and bind it to the ViewModel

The following step is to retrieve data in the ViewModel

public MainViewModel()
{
    var context=new MovieDomainContext();
    var lo = context.Load<Movie>(context.GetMovieQuery());
    lo.Completed += delegate
    {
        Movies =new ObservableCollection<Movie>( lo.Entities);
        OnPropertyChanged("Movies");
    };
}

The default behavior is that only the movies are retrieved. No surprises here. But how do we get the Actors?

Get more data

Open the DomainService and add a method:

public IQueryable<Movie> GetMovieWithActors()
{
    return this.ObjectContext.Movie.Include("MovieActor.Actor");
}

As you can see, the syntax for the Include method is: Include("Table1.Table2")

Open DomainService.metadata and find the properties that should include child entities. Add an [Include] attribute to them:

internal sealed class MovieMetadata
{

    // Metadata classes are not meant to be instantiated.
    private MovieMetadata()
    {
    }

    [Include] // HERE.......
    public EntityCollection<MovieActor> MovieActor { get; set; }

    public int MovieId { get; set; }

    public string Name { get; set; }
}

internal sealed class MovieActorMetadata
{

    // Metadata classes are not meant to be instantiated.
    private MovieActorMetadata()
    {
    }

    [Include] // AND HERE......
    public Actor Actor { get; set; }

    public int ActorId { get; set; }

    public Movie Movie { get; set; }

    public int MovieActorId { get; set; }

    public int MovieId { get; set; }
}

Open your ViewModel and use the GetMovieWithActors query:

public MainViewModel()
{
    var context=new MovieDomainContext();
    var lo = context.Load<Movie>(context.GetMovieWithActorsQuery());
    lo.Completed += delegate
    {
        Movies =new ObservableCollection<Movie>( lo.Entities);
        OnPropertyChanged("Movies");
    };
}

If you place a breakpoint on the OnPropertyChanged line, you can see that the Movies collection holds a list of MovieActors and each MovieActor holds an Actor entity.

This tutorial only focusses on the Many-to-Many data part. If you have questions about MVVM or RIA in general, just email me!

Silverlight Checkbox Slider Style

Download demo project: Checkboxslider.zip

In preparation for a demo tomorrow I created this Silverlight Checkbox Slider
Style
from an empty checkbox template:

Install Microsoft Silverlight

A quick Google showed me there wasn’t such a style available for download so I would like to share it with you. If you only want the style, you can download the zipped CheckBoxSliderStyle.zip and add the following xaml to the Application.Resources node of your app.xaml:

<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="CheckBoxSliderStyle.xaml"/>
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

 

Style more

Feel free to use it any way you like. There are lots of possible style variations. Have fun!

My Windows Phone tutorial in Webdesigner magazine

I wrote a tutorial on how to create a simple Windows Phone ToDo application using Expression Blend.
It is published in the Dutch Webdesigner Magazine (issue 28) wich is available in the stores today!

photo: Monique

I would like to thank Edwin Toonen from Webdesigner Magazine for giving me the opportunity to publish my work.