Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 14 August 2009

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

Posted on 21:37 by Unknown

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.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ▼  August (5)
      • Silverlight 3 : RIA Services for your LOB Applicat...
      • Silverlight 3 Windows 7 GA Countdown for all Windo...
      • Silverlight 3 : RIA Services for your LOB Applicat...
      • Silverlight 3 : RIA Services for your LOB Applicat...
      • Silverlight 3 : Implement Charting – Content is in...
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile