Archive for the ‘RIA’ Category

Silverlight 4 example with MVVM

October 10, 2011 Leave a comment

Hello guys,

Its Monday and now back to work, thought to post an article on what i have grasped so far,

Let me post an example which illustrates the basic of MVVM, i will show the steps how can we bind grid with data from Viewmodel insted of doing that from code behind.

Basic of MVVM: MVVM is a Microsoft’s pattern and practice to apart the logic from view, so it can be vision like a loosely coupled architecture. All the things will work with Binding in designing.

Who can follow MVVM: Frankly speak, Architectures have their pros and cons, If the team wants to work considering future of application in terms of ease of understanding and for integration & testing friendly, strictly follow MVVM. if project is running in short of time and if team don’t have learning curve then it would be better to avoid.

Tools and Technology used:

I am using VS 2010 as development IDE With ReSharper for Code refactoring , WCF RIA service,  Galasoft MVVM light toolkit as MVVM framework.

Galasoft toolkit has many features which will ease your MVVM development, it has APIs for Commanding and Messaging.

You  can also find different MVVM frameworks and compare them to use in your developement here

So lets jump to MVVM:

Firstly i am going to narrate the basic view model integration with XAML view, also commanding which will handle inside the view model,

So, First open VS and create new silverlight business application(check the RIA link is enabled on Silverlight -> Properties)

Now add one folder inside Silverlight project named “Viewmodel”, inside it add one  class name it “EmployeeViewModel”. Implement INotifyPropertyChanged interface and implement the method & event of INotifyPropertyChanged interface.

below should be your code look like,

<br />
private IQueryable employeProp;</p>
<p>public IQueryanle EmployeeProp</p>
<p>get { return  employeProp;}</p>
<p> if(employeProp != value)</p>
<p>employeProp = value;</p>
<p>return;<br />
<p>}<br />

Now we need to get data in this collection, lets add a method in default constructor which will loads data from employee table.
Once it is done move to XAML and add datacontext & bind grid to collection of employee (below are the steps).
There are multiple ways to give Viewmodel context to view. lets go first with XAML designing,

1] Add namespace for view model class xmlns:vm=”clr-namespace:Basic_MVVM_App.ViewModel”
2] Use one of two of below code,

<br />
&lt;UserControl.Resources&gt;<br />
MainViewModel x:Key=&quot;MainViewModel&quot;&gt;<br />
&lt;/UserControl.Resources&gt;<br />

if above one is used then we have to assign datacontext on some control, in my code i have assigned it on Parent Grid

<br />
StaticResource MainViewModel }}&gt;<br />

//MainViewModel is the key name which

is created in Resources. Here Big thing to take care is StaticResource property. if you forget to add you it means binding isn’t established between view and Viewmodel.

b. Use below line in your designing,

<br />
&lt;UserControl.DataContext&gt;<br />
&lt;Binding Source=&quot;{StaticResource MainViewModel}&quot; /&gt;<br />
&lt;/UserControl.DataContext&gt;<br />

if above one is used then there is no need to assign data context on any control.

Another way assigning data context is on code behind like, In Loaded event of your code behind, add below code
Datacontext = new EmployeeViewModel(); // name of your view model

Now add data grid control and on Itemsource property add

ItemsSource=&quot;{Binding Path=PagedView, Mode=TwoWay}&quot;.<br />

Thats it!! you are done with basic example of MVVM.

What is next?

I will post my next article on Commanding to handle event in Viewmodel



Silverlight and RIA services

September 10, 2010 Leave a comment

Silverlight 4.0 release is ships with a new WCF RIA framework,

In context of silverlight application, as they span across client machines and from there they calls service to provide data to user. So we can come up with architecture which is something like below,

(Gap between Client and server side)

On the left side it is Silverlight client application and data access on the right hand side.

Between this two is “the central gap”(Question mark) which has to be cover by some kind of distributed technology and we have several possibilities in silverlight such as the following listed in order they are layered on each other,

1]Via Sockets

2]Via HTTP : This can be possible by passing backward and forward xml.

3]Via Windows communication foundation: There are two ways to communicate with silverlight first is via HTTP and another is Via TCP.

Silverlight is a one possible client for WCF Data Services and the client library does offer some higher level abilities than basic CRUD over entity sets with capabilities such as;

  • Can turn LINQ formed queries into URI based syntax compatible withOData
  • Auto tracking of changes made to entities ( and entity sets ) returned from WCF Data Services to make for automatic submission of changes
  • Can deal with concurrent updates and the errors/retries that are needed to deal with those situations
  • batching of queries/modifications to the server

but the still level of “linking” between client and services is still low.

Consequently, in RIA Services application there is the tooling and framework support for sharing a richer set of objects between the client and the service than you’d see with a solution built around something lower level like a pure WCF solution allowing for a lot less friction in terms of;

  • Ability of server database entity available on client side.
  • Flexibilty for Searialized queries development on client side which will be then available on server for execution purpose.
  • Provives automatic trailing of entity changes on client side.
  • Built in validation mechanism.
  • You can add your any arbitary code on server side for business logic.
  • It provides buiit in services like membership, roles, profile with a Silverlight client.

Note: “ RIA not only support silverlight application but can also work with AJAX applications too.”

There is a nice video on WCF RIA service implementation which you can view from here,
Reference article for getting started:  Getting started

Do you know this about RIA service:

August 31, 2010 3 comments

RIA services strictly follow Naming convention and signatures.Like Method that are meant to query and return data to the client should begin with one of the following prefixes:Get,Fetch,Query,Retrieve and Select.This methods should have return type as IEnumerable, IQueryable, or a single instance of the entity type.However, you can have any number of parameters in a query method, which can help you to filter the results. Below Table lists the various combinations of naming conventions that you can follow when you add your own CRUD operations, if you wish for them to be exposed by the WCF RIA Services framework.

Data method

Required Method Prefix

Required Method Signature

Query Data Get, Fetch, Query, Retrieve,

or Select

IEnumerable<T> GetData()

IQueryable<T> GetData()

Create Data Insert, Add, Create void InsertData(T entity)
Update Data Update, Change, Modify void UpdateData(T entity)
Delete Data Delete, Remove void DeleteData(T entity)

If, for some reason, you are unable to follow the WCF RIA Services naming conventions in your own custom methods, you also have the option of adding specific attributes above the method declarations.

The WCF RIA Services framework also looks for these attributes when determining which methods to make available to clients. You can use any of the following attributes above your custom methods: [Query], [Insert], [Update], and [Delete]. For example, if you have a method that searches for specific foods called FindExercises, by default, this method is not exposed to the client.

If you were to add the [Query] attribute directly above the method declaration then the framework would, in fact, expose it to clients.

Categories: RIA Tags: