Table of Contents
- Introduction
- Background
- Prerequisites
- Step By Step Implementation
- Conclusion
To check out the power of VS2010 is something that most of us are eagerly waiting to see it. Microsoft Tech - Ed in recent times has done a lot to showcase their remarkable products. It had all from SQL Server 2008 R2 to VS2010. The power of Windows Azure and WCF RIA services were the highlight of the show. My objective is to showcase some cool features of VS2010 and help developers to take full advantage of cool features that shipped with VS2010. Microsoft does say, Life runs on code, but here is the example where we can see hardly any code by developer and everything is automated to help one to create a website with delete, add, edit, sort and paging features. These take no code from the developer and it is just a few drag and drop to create a fancy RIA website.
In VS2010 .NET 4.0, you get a whole lot of website templates and Silverlight template websites to start with. You can go ahead and customize this template as per your need. My intent is to help you to use this feature wherein one can create normal add, edit, sorting, paging and delete feature in Grid view within a few minutes. All you get is a readymade code to do this. This is only possible using WCF RIA services.
'Ultimate' version of VS2010 has almost all features to start with.
There are four editions of VS2010:
- Ultimate
- Premium
- Test
- Professional
Supporting Operating Systems
- Windows XP Sp3
- Windows Vista SP2
- Windows 7
- Windows Server 2003 SP 2
- Windows Server 2003 R2
- Windows Server 2008 SP 2
- Windows Server 2008 R2
Tutorial WCF RIA Service RC
Install Silverlight 4 SDK
Given below are the steps to create a web application using WCF RIA services.
Note: One needs to build(CTRL+Shift+B) the application after completion of each stated step.
Add three references of WCF RIA Services:
Microsoft.Web.DomainServices.WebControls
System.ServiceModel.DomainServices.Hosting
System.ServiceModel.DomainServices.Server
Go to Data tab and select ADOEntity Data Model in it. Select database and table. For given example here, I've created a sample customer
table that contains two fields, CustomerID
and CustomerName
.
Build the solution (CTRL+Shift+B).
Now we need to create Domain services (DomainService.cs). Under web menu, select 'Domain Service Class'.The Domain services is a class that has contracts with data access layer. In this example, we've Customer
as a domain class and we've services related to Customer
like AddCustomer
, EditCustomer
and DeleteCustomer
.
Build the solution (CTRL+Shift+B).
Select tblCustomer
entities and click checkbox
to enable 'Editing' feature. Click Ok to proceed further.
Build the solution (CTRL+F5). It will throw a runtime error. As we enabled paging, we need to update few lines of code.
Once we have the data model and domain service ready, we need to bring this model into view layer. For this, we need to drag DomainDataSource
. We need to understand this hierarchy thoroughly. The DomainDataSource
helps to communicate to Domain service and Domain service thus communicates to Data entity model. If you do not find DomainDataSource
control in the toolbox, then reference it in toolbox through the DLL.
Add the following tags in web.config file of the empty website before you proceed to bind this DomainDataSource
with Gridview
Control.The following tag must be added under <System.web>
.
<pages>
<controls>
<add tagPrefix="asp" namespace="Microsoft.Web.UI.WebControls"
assembly="Microsoft.Web.DomainServices.WebControls" />
</controls>
</pages>
Build the solution (CTRL+F5).
Once we have DomainDataSource
in design view, we need to bind this domain control with Domain Service Class.Configure
Datasource as given in the figure below:
Select domain service class and bind the same to DomainDatasource
control. This enables insert, delete and update features.
Drag Gridview
in Empty website and configure Gridview Datasource
with DomainDataSource control.Select
all features like sorting, paging, editing and deleting. This will automate these features within gridview
without you having to write any piece of code.
Now run the application (CTRl+F5). This will throw a runtime error. This is because we enabled paging and for this we need to make some changes in DomainService1.cs file. One needs to go to GetTblCustomer
and comment this part of code:
return this.ObjectContext.tblCustomers;
and add this:
return this.ObjectContext.tblCustomers.OrderBy(p => p.CustomerID);
This will fetch recordset
as per CusotmerID
and keep the page count internally. The code below is an autogenerated one and one can customize it as per the requirement.
public class DomainService1 : LinqToEntitiesDomainService <AdventureEntities>
{
public IQueryable <tblCustomer > GetTblCustomers()
{
return this.ObjectContext.tblCustomers.OrderBy(p => p.CustomerID);
}
public void InsertTblCustomer(tblCustomer tblCustomer)
{
if ((tblCustomer.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState
(tblCustomer, EntityState.Added);
}
else
{
this.ObjectContext.tblCustomers.AddObject(tblCustomer);
}
}
Run the website and you can now update, delete, sort and navigate across recordset
with WCF RIA Service feature.
This demonstration is the first step towards the offering VS2010 WCF RIA and there are many features that can help developers to create wonders. Any suggestions or corrections are most welcome here.