Asp.Net Domain Data Source Control

The Asp.Net team shipped a preview release of Domain Data Source control with .NET RIA Services toolkit. In this post I am going to cover some of the mainline features of the control. The post assumes that you know how to create a Domain Service and hook it up with the Domain Data Source Control. You can refer to the following walkthrough to cover this pre requisite .

Where to get it and install it?

The Domain Data Source control is shipped as part of the toolkit release of .NET RIA Services toolkit. You would need to install the following to use the control.

What is the DomainDataSource control?

The DomainDataSource control gives Asp.NET developers the ability to use .Net RIA Services in building Asp.Net applications. It also allows you to hook your validation logic to the MetaData attributes  and allows easy integrations with Roles and Authentication features of Asp.Net

Why another Data Source?

Most of you must be wondering why another one after a long list of existing data source controls like (access, sql, xml, linq, entity etc). Well the goal was to make the data source control independent of the the underlying data technology being used(Linq/EF etc) and also to make use of the RIA Services pattern for writing a business logic layer.

What do you get in the release?

1. DomainData Source control

2. Dynamic Data project templates(VB and C#) that bring together DynamicData and .NET RIA Services.

Examples of doing custom operations with the Data Source

In this post I am going to cover custom operations that you can do with the Data Source control. For this sample I am using DomainService generated against a Northwind Entity Framework model.

Custom Sorting:

The Domain Data Source controls has a property called SortParameterName which passes in the header value from the data bound control to the query method.

 <asp:GridView ID="GridView1" runat="server" DataSourceID="DDS" AllowSorting="True">
     </asp:GridView>
     <asp:DomainDataSource runat="server" ID="DDS" DomainServiceTypeName="DDS_Post.SimpleDomianService"
         SortParameterName="sortBy" QueryName="GetProductsCustomSorting">
     </asp:DomainDataSource>
 public IQueryable<Product> GetProductsCustomSorting(string sortBy)
         {
             if (sortBy.Equals("CategoryID"))
                 return this.ObjectContext.Products.OrderBy(p => p.CategoryID);
             else if(sortBy.Equals("CategoryID DESC"))
                 return this.ObjectContext.Products.OrderByDescending(p => p.CategoryID);
             else
                 return this.ObjectContext.Products;            
         }

 Custom Paging

The DDS control has properties called  MaximumRowsParameterName(rows per page) and StartRowIndexParameterName(start index). The DomainService needs to have a QueryMethod defined which takes in the above 2 parameters and returns a count of the the result set.

 
<asp:DomainDataSource runat="server" ID="DDS" 

DomainServiceTypeName="DDS_Post.SimpleDomianService"      

MaximumRowsParameterName="rowsPerPage" 

StartRowIndexParameterName="startRow"QueryName="GetProductsCustomPaging"> 

</asp:DomainDataSource>



 public IQueryable<Product> GetProductsCustomPaging(int rowsPerPage, int startRow, out int count)
         {
             count = this.ObjectContext.Products.Count();
             return this.ObjectContext.Products.OrderBy(p=>p.ProductID).Skip(startRow).Take(12);
         }

Restricting access to certain methods of Domain Service to authenticated users

For this sample to work you would have to set up authentication in your website. After setting this up you can add the RequiresAuthentication attribute on your domain service method.

 [RequiresAuthentication()]  
 public IQueryable<Product> GetProducts()
        {
            return this.ObjectContext.Products;
        }
  

When an unauthenticated user tries to access the page which has the domain data source hooked with GetProducts() then Domian Service would throw an exception. As a developer of the website you would have to catch the exception in the onQueried()/OnInserted()/OnUpdated()/OnDeleted() of the data source and write your custom logic of handling the error.

Restricting access to certain methods of Domain Service to users in a certain role

For this sample to work you would have to set up roles in your website. After setting this up you can add the RequiresRoles attribute on your domain service method. The RequiresRoles takes in the list of roles names that are allowed to access the domain method.

 [RequiresRole("Admin")]
 public IQueryable<Product> GetProducts()
 {
     return this.ObjectContext.Products;
 }

When a user who is not in the roles specified in the RequireRoles() tries to access the page which has the domain data source hooked with GetProducts(), then Domian Service would throw an exception. As a developer of the website you would have to catch the exception in the onQueried()/OnInserted()/OnUpdated()/OnDeleted() of the data source and write your custom logic of handling the error.