Saturday, August 15, 2009

Silverlight 3 : RIA Services for your LOB Applications – Part 2

First of all I wish all my Indian friends a very Happy Independence Day !

In my last article, we talked about What are RIA Services and what Silverlight Business Application template is capable of doing. Today in this part, I am going to show you, how easily you can get data to your views and make a data drive application. Binding Gird,DataForms etc. These topics I have already covered in my old articles in much more depth, So I will just show you the way of doing it easily so as to make you feel comfortable with this project template.

Our first job is to add a View and make it visible over Template. You can add a View by just right clicking on View Folder and say Add New Item and Simply Add Silverlight Control like we do normally in our Silverlight Projects. Once we done it we can see the newly added Employee View like this :

employee

Now we will add a DataGrid control to it so as to bind some data from Database once we click on Employee Tab, so XAML code is like this :

<TextBlock Style="{StaticResource HeaderTextStyle}" HorizontalAlignment="Left"
           Margin="8,8,0,19" Width="212" Text="Employee List" TextWrapping="Wrap"/>      
        <data:DataGrid x:Name="empGrid" Grid.Row="1"/>

Once you have grid in place then next step is to add Data, We are going to add ADO.NET Entity Model for this like we did in past for several occasions. So all you need is to just right click on your Web Project and Add New Item as ADO.NET Entity Data Model like this :

edmx

Now we have Data Model in place, but still how to query our data and fetch it is still a question, So now for that we will add up “Domain Service Class”. Domain Service Class basically builds metadata type like structure for you, So once you associate your Tables to it, It will create CRUD Methods (i.e Basic Data Operations like Insert,Update,Delete) for you automatically like this :

domainservice

dmnsvrc

So you will find following things in Domain Service Class which are generated automatically for you like this :

[EnableClientAccess()]
   public class NwdDomainService : LinqToEntitiesDomainService<NorthwindEntities>
   {

       // TODO: Consider
       // 1. Adding parameters to this method and constraining returned results, and/or
       // 2. Adding query methods taking different parameters.
       public IQueryable<Employees> GetEmployees()
       {
           return this.Context.Employees;
       }

       public void InsertEmployees(Employees employees)
       {
           this.Context.AddToEmployees(employees);
       }

       public void UpdateEmployees(Employees currentEmployees)
       {
           this.Context.AttachAsModified(currentEmployees, this.ChangeSet.GetOriginal(currentEmployees));
       }

       public void DeleteEmployees(Employees employees)
       {
           if ((employees.EntityState == EntityState.Detached))
           {
               this.Context.Attach(employees);
           }
           this.Context.DeleteObject(employees);
       }

}

However this is not at all sufficient if we talking about LOB applications since we many time need customize fetch data based on our own queries, So to handle this, IQueryable<> interface is provided to us, This takes Context as type, so in our case it is like IQueryable<Employees>. So for our own custom query we make use of this along with Linq as and when required, So for example if I want to order my data by region while loading the Grid, I can write my own method in this class like this :

public IQueryable<Employees> GetEmployeesByName()
{
         return this.Context.Employees.OrderBy(e => e.Region);
}

Still if you want exact meaning of IQueryable<> :

 

Provides functionality to evaluate queries against a
specific data source wherein the type of the data is known

But since all this logic is present in Domain Service Class, so we can’t directly bind it unless we create instance of it in our respective Views, So now lets move ahead to Employee View where I have already taken one data grid, Now to bind that I will use following code :

void EmployeeListing_Loaded(object sender, RoutedEventArgs e)
{
      NwdDomainContext nwd = new NwdDomainContext();

      nwd.Load(nwd.GetEmployeesByNameQuery());

      empGrid.ItemsSource = nwd.Employees;           
}

Remember, you need to add the respective reference else it will not allow you to access data. Load() will take care of loading data fired by particular method which we wrote in Domain Service Class and then we will simple bind this to grid and after executing this we will get output like this :

finalgrid

This is how you can bind your data simply and make use of any control, All you need is Design the View and write corresponding method in Domain Service Class which is capable of doing many things (For More info : Please go through RIA Services Documentation)

You try this new thing out and let me know your feedback, Since this is new template for many one, One might feel that why to go for this if we can do this anyhow in Silverlight project without any template,But once you start doing customization then you will realize the power of RIA Services and Total Time saved by using this template. Though I am done over here with the basic use of this Template, Still I am adding one more part to this series which will be sort of discussions and what all things can be done including Reporting in Silverlight, I will also share some good resources which are already made ready by enthusiastic people like you, So keep looking for final part which will fulfill all the things we need in real LOB applications.

Vikram.

2 comments:

Shibu Narayanan said...

Thanks Vikram,
It was a quite good article showing comprehensively how to build Siverlight App using RIA Service...
sad

Vikram Pendse said...

@Shibu : Thanks for your comment, Soon I will be covering end-to-end example on RIA,Right from Design phase to Deployment.