This post is a preview of the upcoming documentation which describes a new approach for accessing data from an ASP.NET client (Web page) by using the WCF RIA Services domain service. The complete documentation will be available on MSDN.
It is important to notice that the domain service enables you to detach your application from the specific database model. This gives you the advantage of focusing on the business logic and on creating code that is easily portable to a different model.
ASP.NET provides a DomainSourceControl control that can be used by data-bound controls, such as the System.Web.UI.WebControls.GridView control, to access the database and enable the user to perform creates, read, update and delete (CRUD) operations.
The RIA Services domain service provides a pattern to write application logic that runs on the server and controls access to data for queries, changes, and custom operations. It also provides end-to-end support for common tasks such as data validation, authentication and roles by integrating Silverlight client and ASP.NET server applications.
Even though this walkthrough describes the domain service in ASP.NET, the use of the service is not limited to ASP.NET applications. This walkthrough does not address the integration of ASP.NET server with Silverlight client applications.
This topic contains the following procedures:
You need the following components to complete this walkthrough:
A Visual Studio project with source code is available at this location: Using Domain Service.
To use the domain service, you must create an ASP.NET Web application capable of interacting with a database. The following are the steps you must perform:
The following procedure shows how to create an ASP.NET Web application.
<pages> <controls> <add tagPrefix="asp" namespace="System.Web.DomainServices.WebControls" assembly="System.Web.DomainServices.WebControls" /> </controls> </pages>
This configuration definition enables you to use the <asp:DomainDataSource> control in a Web page as described later in the "To declare the DomainDataSource control in a page" procedure.
The ADO.NET Entity Data Model Designer is displayed. You have created the data model that represents the AdventureWorksLT database. Notice that the system.config file is updated to contains the connection string and the proper DLL reference as shown next:
<assemblies> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </assemblies>
<connectionStrings> <add name="AdventureWorksLT_DataEntities" connectionString="metadata=res://*/AdventureWorksLT.csdl|res://*/AdventureWorksLT.ssdl|res://*/AdventureWorksLT.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AdventureWorksLT_Data.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings>
Building makes the AdventureWorksLT_DataEntities context class available to the domain service in the next procedure.
This section describes the steps that you must perform to use the domain service in an ASP.NET application. These steps include the following:
The following steps show how to customize the AdventureWorksDomainService class and the related metadata file to include your business logic. The customization is very simple, but gives you an idea of the modifications you can make.
public void UpdateProduct(Product currentProduct) { if ((currentProduct.EntityState == EntityState.Detached)) { // Custom logic: set a lower limit for the price. if (currentProduct.ListPrice < 5) // Throw custom exception. throw new ValidationException(new ValidationResult("The list price must be >= 5.", new[] { "ListPrice" }), null, null); this.ObjectContext.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct)); // Custom logic: set the date to the current value. currentProduct.ModifiedDate = DateTime.Today; } }
Public Sub UpdateProduct(ByVal currentProduct As Product) If (currentProduct.EntityState = EntityState.Detached) Then ' Custom logic: set a lower limit for the price. If currentProduct.ListPrice < 5 Then ' Throw custom exception. Throw New ValidationException(New ValidationResult("The list price must be >= 5.", _ New String() {"ListPrice"}), Nothing, Nothing) End If Me.ObjectContext.AttachAsModified(currentProduct, Me.ChangeSet.GetOriginal(currentProduct)) ' Custom logic: set the date to the current value. currentProduct.ModifiedDate = DateTime.Today End If End Sub
When the validation fails, an exception is raised and an error message is sent to the page to be displayed to the user.
// Custom logic: make Color a required field. [Required(AllowEmptyStrings = false, ErrorMessage = "Color is required")] public string Color;
' Custom logic: make Color a required field. <Required(AllowEmptyStrings := False, ErrorMessage := "Color is required")> _ Public Color As String
The following steps show how to declare the DomainDataSource control in a page markup so that the user can interact with the database.
<asp:DomainDataSource ID="DomaninDataSourceID" EnableDelete="true" EnableInsert="true" EnableUpdate="true" DomainServiceTypeName="UsingDomainService.AdventureWorksDomainService" SelectMethod="GetProducts" runat="server" > </asp:DomainDataSource>
<asp:GridView ID="GridView1" DataSourceID="DomaninDataSourceID" CssClass="DDGridView" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="ProductID,ProductNumber,StandardCost,Size,Weight,ProductCategoryID, ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,ThumbNailPhotoFileName,rowguid" AllowSorting="true" AllowPaging="True" > <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:TemplateField> <HeaderTemplate>Color</HeaderTemplate> <ItemTemplate> <%#Eval("Color")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="ColorID" runat="server" Text='<%#Bind("Color")%>'></asp:TextBox> <asp:DomainValidator CssClass="DDValidator" runat="server" DataField="Color"></asp:DomainValidator> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <HeaderTemplate>ListPrice</HeaderTemplate> <ItemTemplate> <%#Eval("ListPrice")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="ListPriceID" runat="server" Text='<%#Bind("ListPrice")%>'></asp:TextBox> <asp:DomainValidator CssClass="DDValidator" runat="server" DataField="ListPrice"></asp:DomainValidator> </EditItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView>
<asp:ValidationSummary runat="server" CssClass="DDValidator" /> <asp:DomainValidator runat="server" Visible="true" ControlToValidate="GridView1"></asp:DomainValidator>
This section shows how to test that the domain service capabilities through the use of the GridView control. This procedure verifies the following:
How I use the DomainService in a code behind?
IC Components http://www.hqew.net
electronic components http://www.partinchina.com