Using DomainDataSource in ASP.Net

11 באפריל 2009

4 תגובות

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!

הוסף תגובה
facebook linkedin twitter email

4 תגובות

  1. nick_trerel30 ביוני 2009 ב 12:00

    http://zelroldron.com/calida.html ractroctrv
    ractroctrv
    [link=http://basboc.com/vierlil.html]ractroctrv[/link]
    [url=http://ctrboc.com/getgetri.html]ractroctrv[/url]
    http://eltorlipascnare.com/roacboctrouc.htm c4torout boerrelgett
    letonoel
    http://crolladarsitbod.com/eltacel.htm ccobocalvard orletoccna
    oloouelrolvi
    http://cnaalbaselbo
    elracerlad

  2. Bill McKnight18 במאי 2010 ב 6:55

    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?

  3. TexasBelle22 במאי 2010 ב 1:26

    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"
    %>

  4. riley27 ביולי 2011 ב 4:17

    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.

Comments are closed.