Asp.Net Domain Data Source Control

Asp.Net Domain Data Source Control

  • Comments 1

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.

protected void DDS_Queried(object sender,Microsoft.Web.UI.WebControls.DomainDataSourceQueriedEventArgs e)        
{
      var exception = e.Exception as UnauthorizedAccessException; 
      if (exception != null){
                Response.Redirect("Login.aspx");
      } 
}

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.

Leave a Comment
  • Please add 4 and 2 and type the answer here:
  • Post
  • I first create ASP.NET Dynamic Data Domain Service application, which connect to NorthWind Database. It works like usual. Then following your sample code to add IQueryable<Product> method:

    “public IQueryable<Product> GetProductsCustomSorting(string sortBy)”

    But run default.aspx gets error

    “Cannot find the default query method for type ‘XXX(my type' in the domain service ‘XXXService(My service)'. When a type has multiple query methods associated with it then one of them must be marked as the default one using QueryAttribute.”

    Why? How to set default query method?

Page 1 of 1 (1 items)