RIA Integration

Oct 15, 2010 at 1:51 PM

Hello gang,

I'm new to Silverlight and CM, but not new to software design principles in general, so pardon the ignorance. I've read the documentation about a million times (give or take few) but still seem to be missing something. I am trying to create a Silverlight application that leverages RIA. I've read the thread at http://caliburnmicro.codeplex.com/Thread/View.aspx?ThreadId=227621 and have built something almost exactly like the "Model" demo in the following documentation http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=Documentation. The problem I am having is that when I bind my ItemsControl utilizing the functionality that janoveh contributed above, some weird things happen. My GetEmployees method/"action" (if I believe I'm using the correct terminology) throws a NullReferenceException, and THEN the GetEmployees RIA query will fire. Here is what I currently have coded. Again, forgive my ignorance as I get up to speed.

XAML:

<UserControl x:Class="Silverlight.Shell.View.EmployeeView" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             mc:Ignorable="d" d:DesignHeight="283" 
             xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
             d:DesignWidth="531">
    <StackPanel x:Name="LayoutRoot" Background="White" Height="282" Width="532">
        <ItemsControl x:Name="GetEmployees">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button Content="Remove"  
                                cal:Message.Attach="Remove($dataContext)" />
                        <TextBlock Text="{Binding FirstName}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

ViewModel (DataLoader<T> is exactly like janoveh's implementation from above):

using Caliburn.Micro;
using Web.Model;
using Web.Service;

namespace Silverlight.Shell.ViewModel
{
    public class EmployeeViewModel : PropertyChangedBase
    {
        #region Private/Protected Fields

        private readonly EmployeeDomainContext theContext;
        private readonly DataLoader<EmployeeDTO> theDTOLoader;

        #endregion

        #region Public Constructors

        public EmployeeViewModel()
        {
            theContext = new EmployeeDomainContext();
            theDTOLoader = new DataLoader<EmployeeDTO>(theContext, theContext.GetEmployeesQuery());

            theDTOLoader.Execute();

            theDTOLoader.Completed += DTOLoader_Completed;
        }

        #endregion

        #region Public Properties

        public BindableCollection<EmployeeDTO> GetEmployees
        {
            get { return new BindableCollection<EmployeeDTO>(theContext.EmployeeDTOs); }
        }

        #endregion

        #region Private Methods

        private void DTOLoader_Completed(object sender, ResultCompletionEventArgs e)
        {
            //Not even sure what to do here?
        }

        #endregion
    }
}
Thanks in advance!
Junoti
Oct 15, 2010 at 3:50 PM

Hi, Junoti!

The following minimalistic example viewmodel should point you in the right direction, I believe:

    public class Countries2ViewModel : Screen
    {
        private readonly DomSvc _theContext = new DomSvc();

        public EntitySet<Country> Countries
        {
            get { return _theContext.Countries; }
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            Coroutine.Execute(FillCountries());
        }

        public IEnumerator<IResult> FillCountries()
        {
            yield return new LoadData<Country>(_theContext, _theContext.GetCountriesQuery());
        }

        public void Remove(Country country)
        {
            // Omitted here...
        }
    }
XAML:
<UserControl x:Class="CMTrainer.Views.Countries2View" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             mc:Ignorable="d" d:DesignHeight="283" 
             xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
             d:DesignWidth="531">
    <StackPanel x:Name="LayoutRoot" Background="White" Height="282" Width="532">
        <ItemsControl x:Name="Countries">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button Content="Remove"  
                                cal:Message.Attach="Remove($dataContext)" />
                        <TextBlock Text="{Binding CountryName}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

I hope this help.  
You could also check out my WCF RIA Services version of the, now famous, GameLibrary sample here: 
http://caliburnmicro.codeplex.com/SourceControl/network/Forks/janoveh/CaliburnMicroJanovehFork/changeset/view/40ec9132a739
You will find it under the "Samples" catalog.

Oct 16, 2010 at 3:09 AM

Janoveh,

Next to Rob, you are a genius! Thanks for your help with this and the sample you have provided! Do you have any thoughts/likes/dislikes about integrating with RIA?

Junoti

Oct 16, 2010 at 11:16 AM
Edited Oct 16, 2010 at 11:18 AM

Hi, Junoti!

Thank you very much!

I have been using RIA Services since its early start little over one year ago.  I actually started using it in a production solution while still in preview mode (!).

So you could call me a big fan.  However, I think to really love RIA Services you need to know it well enough to know how to tweak it, if you see what I mean.

One of my latest "achievements" in that direction is to run MDX queries againts Analysis Services, from Silverlight, through RIA Services.  A god example of tweaking. 

In some scenarios I use RIA Services only as a POCO transfer infrastrucure between client and server, somtimes without a datasource behind at all.

RIA Services with Caliburn Micro is a perfect match, in my mind.  My discovery of Rob's coroutine implementation was a magical moment. 

I had been strugling up until then with good strategies for orchestrating RIA Services calls.  I had come to realise a state-machine sort of solution was needed.  Rob's video from MIX10 was a mind-blower.

So, since then, CM and RIA Services are close mates of mine. We play together almost every day. :-)

I am currently working on a project of some magnitude where these two are matched.

 

Oct 16, 2010 at 6:41 PM

Janoveh,

Thank you very much for your insight. You have been extremely helpful!