DCSIMG
Using DomainDataSource in ASP.Net - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

Using DomainDataSource in ASP.Net

Using DomainDataSource in ASP.Net

DomainDataSource ASP.Net

In ASP.Net 2.0 we were introduced to this concept of Data Source controls. We got the ObjectDataSource, XmlDataSource and SqlDataSource that let us bind a GridView or a ListBox to some data without having to write any additional code. In .Net Framework 3.5 we got LinqDataSource and with .Net Framework 3.5 – the EntityDataSource. In the future of ASP.Net, among other significant improvements around data access, we get a new way of accessing our data – DomainDataSource.

The benefits of using the DomainDataSource is that is give us nice separation from the way our Data Model is represented (LINQ to SQL vs EF, POCO and Cloud) and gives us more control over how the data is being retrieved and manipulated.

In this post I’ll introduce the DomainDataSource that is already available as part of .Net RIA Service, and give a quick walkthrough on how to use it in an ASP.Net Web Forms application.

Creating a Data Model and a Domain Service

DomainDataSource ASP.NetCreate a new ASP.Net Web Application in Visual Studio 2008.

Add a Data Model to your application, whether it is a LINQ to SQL data model, an Entity Framework Data Model, or any other data model. In this sample I am using the Bank Schema with LINQ to SQL.

Make sure to build the project so that Visual Studio will generate the data classes and data context before the next step.

Add a new Domain Service. Add a new Item to the server project, and select the Domain Service template in the Web category.

DomainDataSource ASP.Net

After you add this item, the New Domain Service Class Dialog is shown. Select the Data Context (BankDataContext in this sample), select the entities you want to expose and whether you want to allow editing and click OK.

DomainDataSource ASP.Net

This adds the BankDomainService.cs that contains the code that exposes the data to the client, and BankDomainService.metadata.cs that contains additional metadata, mostly for presentation and validation.

This is how the DomainService looks like when working with LINQ to SQL. If you were working with the Entity Framework, the base class would be different. It simply contains CRUD methods for working with the data context.

public class BankDomainService : LinqToSqlDomainService<BankDataContext>

{

  public IQueryable<Customer> GetCustomers()

  {

    return this.Context.Customers;

  }

 

  public void InsertCustomer(Customer customer)

  {

    this.Context.Customers.InsertOnSubmit(customer);

  }

 

  public void UpdateCustomer(Customer currentCustomer, Customer originalCustomer)

  {

    this.Context.Customers.Attach(currentCustomer, originalCustomer);

  }

 

  public void DeleteCustomer(Customer customer)

  {

    this.Context.Customers.Attach(customer, customer);

    this.Context.Customers.DeleteOnSubmit(customer);

  }

}

It also adds some new references to System.ComponentModel.DataAnnotations, System.Web.DomainServices, System.Web.DomainServices.Providers and System.Web.Ria.

In addition to that, a new Http Handler was added to the web.config file.

<httpHandlers>

  ...

  <add path="DataService.axd"

      verb="GET,POST"

      type="System.Web.Ria.DataServiceFactory, System.Web.Ria,
            Version=2.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35
"

      validate="false" />

</httpHandlers>

Use the DomainDataSource with ASP.Net

In a page of your choice, add a GridView control or any control that can bind to a DataSourceControl.

<asp:GridView runat="server" ID="GridView">

</asp:GridView>

Add a reference to System.Web.DomainServices.WebControls.dll, and in your page, register the prefix for this namespace:

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

Add a DomainDataSource and bind it to the Grid. Notice the in the DomainDataSource declaration I referenced the type of the DomainService with its full namespace, and added which method in it should be used to retrieve data.

<asp:GridView runat="server" ID="GridView"

  AutoGenerateColumns="true"

  DataSourceID="customersDataSource">

</asp:GridView>

 

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

  DomainServiceTypeName="Samples.Bank.Domain.BankDomainService"

  SelectMethod="GetCustomers">

</asp:DomainDataSource>

If you now run the application, you’ll see the grid populated with the results.

ASP. Net DomainDataSource

With DomainDataSource you get more control on how your data is retrieved and manipulated. You can add your logic to the select method, for example – providing a filter by the authenticated user, and add logic in the update methods, such as adding default values before updating the database.

Enjoy!

Comments

Using DomainDataSource in ASP.Net - Guy Burstein's Blog | Webmaster Tools said:

Pingback from  Using DomainDataSource in ASP.Net - Guy Burstein&#39;s Blog | Webmaster Tools

# April 12, 2009 3:31 AM

Using DomainDataSource in ASP.Net - Guy Burstein's Blog said:

Pingback from  Using DomainDataSource in ASP.Net - Guy Burstein&#39;s Blog

# April 12, 2009 5:07 AM

Guy Burstein's Blog said:

ASP.Net DomainDataSource with Select Parameters Continuing with .Net RIA Services with Silverlight and

# April 12, 2009 10:11 AM

Guy Burstein said:

ASP.Net DomainDataSource with Select Parameters Continuing with .Net RIA Services with Silverlight and

# April 12, 2009 10:19 AM

Guy Burstein's Blog said:

ASP.Net QueryExtender Control and DomainDataSource Just another post about .Net RIA Services with Silverlight

# April 12, 2009 11:51 AM

Tune Up Your PC » Post Topic » ASP.Net DomainDataSource with Select Parameters said:

Pingback from  Tune Up Your PC  &raquo; Post Topic   &raquo; ASP.Net DomainDataSource with Select Parameters

# April 13, 2009 2:39 AM

Summaries 11.04.2009 – 13.04.2009 « Bogdan Brinzarea’s blog said:

Pingback from  Summaries 11.04.2009 &ndash; 13.04.2009 &laquo; Bogdan Brinzarea&#8217;s blog

# April 14, 2009 12:26 PM

nick_trerel said:

# June 30, 2009 12:00 PM

Community Blogs said:

Since a major goal of Dynamic Data is to separate the business logic from the user interface, let’s look

# November 30, 2009 5:39 PM

Where does this go? Applying SoC to dynamic data ??? Part 3 | I love .NET! said:

Pingback from  Where does this go? Applying SoC to dynamic data ??? Part 3 | I love .NET!

# November 30, 2009 5:40 PM

Bill McKnight said:

Can't find the System.Web.DomainServices.WebControls.dll in the newest RIAServices.msi for Silverlight 4 / VS2010.  Can you explain how to obtain it?

# May 18, 2010 6:55 AM

TexasBelle said:

re:  Bill McKnight

>> Can't find the System.Web.DomainServices.WebControls.dll

Use Microsoft.Web.DomainServices.WebControls.dll in the April 2010 Wcf Ria SDK. And use

<%@ Register

TagPrefix="ria"

Namespace="Microsoft.Web.DomainServices.WebControls"

Assembly="Microsoft.Web.DomainServices.WebControls"

%>

# May 22, 2010 1:26 AM

riley said:

This works for RIA services sp1 for Silverlight 4 (stable, no release candidates, betas or whatnot):

1. Reference C:\Program Files\Microsoft SDKs\RIA Services\v1.0\Toolkit\Libraries\Server\Microsoft.Web.DomainServices.WebControls.dll in your project.

1a. If you didn't find that file there, dload RIA services again from MS site and use repair option.

2. Add this to your aspx (note different assembly and namespace):

<%@ Register TagPrefix="ria" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.DomainServices.WebControls" %>

3. It might eventually get all f'd up again with new release.

# July 27, 2011 4:17 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: