Telerik blogs

In July Microsoft released a preview of its upcoming Silverlight 3 RIA Services Framework which comes along with a new data source control – DomainDataSource. The DomainDataSource introduces a new way of accessing data by giving us a nice separation from the particular representation of our Data Model and more control over how the data is being retrieved or manipulated.

This post will provide you with a quick walk-through on how to use the DomainDataSource with our RadGrid control.

Install Microsoft .NET RIA Services July 2009 Preview

As mentioned above the DomainDataSource control is part of the MS .NET RIA Services Framework, so you need to first download and install the latter. Before proceeding to the installation of the RIA Services, however, you should make sure that Microsoft Silverlight 3 Tools for Visual Studio 2008 SP1 are installed on your machine. After having completed all installation requirements, we are now ready to go on to our sample web application.

Create a Data Model

Open Visual Studio 2008, create a new ASP.NET Web Application and add a Data Model to it – I have chosen to work with LINQ TO SQL against the Northwind database: 

NorthwindDataContext

Before continuing to the next step you need to build the project so that the data classes and data context be generated and available to be exposed to the Domain Service.

Add a Domain Service

Add a new item to the web application and select the Domain Service template under the Web category:

NorthWindDomainService

From the domain service dialogue select the NorthwindDataContext and choose the Customer entity as well as any other entities for which you may want Visual Studio to generate code that exposes the data:

NorthWindDomainServiceDialogue

The data access code that consists of CRUD methods to work with the data context is contained in the NorthwindDomainService.cs  and with LINQ TO SQL looks as follows:

public class NorthwindDomainService : LinqToSqlDomainService<NorthwindDataContext>  
{  
 
 public IQueryable<Customer> GetCustomers()  
    {  
 return this.Context.Customers;  
    }  
 
 public void InsertCustomer(Customer customer)  
    {  
 this.Context.Customers.InsertOnSubmit(customer);  
    }  
 
 public void UpdateCustomer(Customer currentCustomer)  
    {  
 this.Context.Customers.Attach(currentCustomer, this.ChangeSet.GetOriginal(currentCustomer));  
    }  
 
 public void DeleteCustomer(Customer customer)  
    {  
 this.Context.Customers.Attach(customer);  
 this.Context.Customers.DeleteOnSubmit(customer);  
    }     
 

 

Bind RadGrid to DomainDataSource

Make sure that your application has a reference to the Microsoft.Web.Extensions and System.Web.DomainServices.WebControls assemblies.

Open the default.aspx web form and include the following register directive to the page:

<%@ Register TagPrefix="asp" Namespace="System.Web.DomainServices.WebControls" 

                             Assembly="System.Web.DomainServices.WebControls" %> 

 

Add the following mark-up for the DomainDataSourceControl:

 

<asp:DomainDataSource ID="CustomersDomainDataSource" DomainServiceTypeName="RadGridAndDomainDataSource.NorthwindDomainService" 
   SelectMethod="GetCustomers" EnableDelete="true" EnableInsert="true"  
   EnableUpdate="true" runat="server"
</asp:DomainDataSource> 
 

 

Note that the DomainDataSource should specify the full namespace name for its DomainServiceTypeName property as well as the method that should be used to retrieve the data.

And last but not least, declare the RadGrid control:

    <telerik:RadGrid ID="CustomersGrid" runat="server" Skin="Vista" AllowPaging="true"  
        DataSourceID="CustomersDomainDataSource">   
        <MasterTableView DataSourceID="CustomersDomainDataSource" CommandItemDisplay="Top"  
            AllowAutomaticInserts="true" AllowAutomaticDeletes="true" AllowAutomaticUpdates="true"  
            AutoGenerateColumns="false" DataKeyNames="CustomerID">   
            <Columns>  
                <telerik:GridEditCommandColumn ButtonType="ImageButton">   
                </telerik:GridEditCommandColumn>  
                <telerik:GridBoundColumn DataField="CustomerID" HeaderText="Customer ID" UniqueName="CustomerID"  
                    SortExpression="CustomerID">   
                </telerik:GridBoundColumn>  
                <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company Name" UniqueName="CompanyName"  
                    SortExpression="CompanyName">   
                </telerik:GridBoundColumn>  
                <telerik:GridBoundColumn DataField="ContactName" HeaderText="Contact Name" UniqueName="ContactName"  
                    SortExpression="ContactName">   
                </telerik:GridBoundColumn>  
                <telerik:GridBoundColumn DataField="City" HeaderText="City" UniqueName="City" SortExpression="City">   
                </telerik:GridBoundColumn>  
                <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete">   
                </telerik:GridButtonColumn>  
            </Columns>  
        </MasterTableView>  
    </telerik:RadGrid> 

 

And this is all that is needed to have RadGrid bound to a DomainDataSource control and automatically perform CRUD operations against its underlying data repository.

Download sample code (WCF RIA Services for Silverlight 3.0 & Visual Studio 2008): RadGridAndDomainDataSource

Download sample code (WCF RIA Services for Silverlight 4.0 & Visual Studio 2010): HierarchicalRadGridWithDomainDataSource


About the Author

Iana Tsolova

is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.

Comments

Comments are disabled in preview mode.