I have been playing with the WCF RIA Services (erstwhile .NET RIA Services) for sometime and found that most of the samples out there focus on Silverlight based applications. While the new WCF RIA Services preview for VS 2010 is awesome in terms of its Silverlight integration, I also wanted to test out on building plain vanilla ASP.NET Applications and using the power of WCF RIA Services to build a middle tier for the same.
Ok, to begin with, I already had Visual Studio 2010 Beta 2 installed and went ahead and installed the WCF RIA Services Preview for Visual Studio 2010 Beta 2 (note that, if you already have the WCF RIA Services for Visual Studio 2008 SP1 installed, this doesn’t install on top of it – so you have to chose whether to use the one that works with Visual Studio 2008 with SL3 or VS 2010 Beta 2 with SL4 Beta – i chose the latter)
Once I had these installed, I went ahead and created an “File – New Project – Empty ASP.NET Web Application” in Visual Studio 2010 Beta 2. This creates a blank ASP.NET Web Application.
I went ahead and added an “ADO.NET Entity Data Model” giving it a name “Northwind.edmx” and configured it to use my sample northwind database. In the Entity Design Wizard, for choosing tables, I chose Products, Categories & Suppliers tables alone and completed the steps to create an Entity model.
This gave an Entity Data Model with 3 tables as well as the auto generated Designer.cs file with Context and Entities.
Now, in normal cases, you have seen a lot of demos where we just add an ASP.NET Webform, drag and drop a grid view control and configure it to use the Entity DataSource template which in turn is configured to use the Entity model created in the above steps. With a few additional clicks to enable paging, sorting, editing, deleting etc., your complete Grid with CRUD operations would be ready. We could also do this with SQL DataSource, LINQ DataSource, Object DataSource etc., based on the preference.
The general concern in these approaches were that, there is no actual middle tier and the UI Layer is directly bound to the Data Access Layer. The moment, you add a middle tier, then the flexibility of binding the Entity Model / LINQ to SQL Model etc., becomes obsolete and one has to configure all the UI => Middle tier => Data Access layer steps manually.
Now, with the power of WCF RIA Services, one can actually generate a middle tier with the underlying Entity Layer.
The next thing I did was to add a “Domain Service Class” (this comes only when you have installed the WCF RIA Services preview for VS 2010) and provide it a name, NorthwindBL.cs. Note before doing this, you have to build the solution after creating the Entity Framework model so that the entity model is available for the WCF RIA Service template to pick up and generate service methods.
Once I provided the name for the “Domain Service Class” and clicked “Add” it provided the screen for choosing the available Data Contexts and the entities (the tables we chose to create the entity model). I selected all the three tables (Products, Categories & Suppliers) and checked “Enable Editing” just for the Products Entity. I also unchecked the “Enable Client Access” checkbox in the top since that is specific to Silverlight scenario and checked the “Generate associated classes for metadata” in the bottom. This provided a class file with all the CRUD operations for “Products” and Get methods for “Suppliers” and “Categories” Entities.
Note that these methods are completely customizable and provides the platform for adding custom business logic, which was always missing earlier while binding the UI Layer directly using SQL DataSource, LINQ DataSource etc., For example, one of the generated Get method for Products is as below
public IQueryable<Product> GetProducts()
{
return this.ObjectContext.Products;
}
I wanted to make a simple customization to it by filtering out the product list with a where condition. I could use a LINQ Query and modify the above method as following:-
public IQueryable<Product> GetProducts()
{
return this.ObjectContext.Products.Where(p=>p.ReorderLevel>10);
}
while this is not the greatest business condition, you can get an idea on the amount of customization I could do even for simple crude operation. In addition, I can also add custom methods to this class file.
The next step is to start building the UI Layer. I added a simple Webform to the project and added a GridView from design view. For now on, there are a few tweaking steps that needs to be followed to be able to use the WCF RIA Services.
Once you install the WCF RIA Services, you will get a bunch of assemblies in the locaiton C:\Program Files\Microsoft SDKs\RIA Services\v1.0\Libraries\Server Out of these, you would require the System.Web.DomainServices.WebControls.dll file to be able to use the DomainDataSource control in the Webform.
Add a reference to it in the project as well as try adding the same to the Toolbox by rightclick on Toolbox, Choose Items and browsing to the above folder. Once you do that, you can drag and drop a DomainDataSource to the webform. It automatically adds the Register Tag Prefix as well.
Once this step is done, you can either go to design view and choose the GridView’s configuration set up and choose DomainDataSource under DataSource configuration. That’s all works as of now. It doesn’t take further to the available methods, etc., as it would be case with EntityDataSource, SQL DataSource etc.,
Then, you can manually modify the DomainDataSource to add further properties, as below:-
<cc1:DomainDataSource ID="DomainDataSource1" runat="server" DomainServiceTypeName="<ProjectName>.NorthwindBL"
EnableInsert="true" EnableUpdate="true" EnableDelete="true" SelectMethod="GetProducts" StoreOriginalValuesInViewState="true"></cc1:DomainDataSource>
For the GridView, a few properties needs to be enabled and with that and selecting a template, the code for GridView looks, as below:-
<asp:GridView ID="GridView1" runat="server" DataSourceID="DomainDataSource1"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"
AllowSorting="True" AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
With that, we are good to run the page. Note that, the page retrieves records and binds on the gridview only for values where reorderlevel is greater than 10 as per the cutomization we made to the GetProducts method in the Businesslayer.
Similarly, the Edit, Update operations work automatically but you can go to the respective methods in NorthwindBL and add a few conditions, checks as and where required. You can also add Authorization to make sure that only those folks are allowed to modify etc., but I am not covering them as a part of this post.
So, with minimal steps, we could build a three tiered application skeleton and this can be enhanced greatly to build a full fledged app without you having to manually write out the Service Layer methods.
I have uploaded the sample solution along with this post and you can download the same from the link below. Note that you would need to add your connection string to the Web.config file.
I will try and cover the other scenarios as well in future posts, but for now, we have a 3 tier application built effortlessly with the power of ASP.NET, WCF RIA Services and Entity Framework.
Cheers !!!