Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

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 !!!

posted @ Monday, December 14, 2009 8:09 AM

Print

Comments on this entry:

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Sathyan at 12/14/2009 9:40 PM
Gravatar
Very Nice. So this proves once again that imagination is the barrier.

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by air max at 12/15/2009 3:50 PM
Gravatar
thanks for sharing
AIR MAX TN10

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by as at 12/18/2009 6:37 PM
Gravatar
<div style="z-index:100;width:1100px;height:1100px">
Blog
</div>

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Bryan Reynolds at 12/19/2009 11:12 AM
Gravatar
Can you using the domain data source in a project your referrence as a service?

Meaning the domain service is not in your project, its just referrence through WCF?

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by ronidon at 1/3/2010 5:20 AM
Gravatar
The visual studio is very frequenlty used in may application and you can find its application on films industry nowadays.

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Todd at 1/27/2010 10:27 AM
Gravatar
I was thinking the exact same thing, thanks for the leg work

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by ed hardy at 1/29/2010 1:08 AM
Gravatar
thanks for sharing
ed hardy

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by cheap ugg boots at 1/30/2010 7:24 PM
Gravatar
Robyn Weinstein and Matt Goldich were married cheap ugg Saturday evening in Wilmington, Del. Rabbi Josh Snyder officiated at the Hotel du Pont.

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by slr camera lenses at 2/10/2010 10:53 PM
Gravatar
I was thinking the exact same thing,
fat burning diet

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Freelance Writer Jobs at 2/17/2010 12:33 AM
Gravatar
Me too as well!

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Energy at 2/27/2010 11:17 PM
Gravatar
So many new things, updates and everything in 2010 - this year already rocks hard!

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by 英雄合击 at 3/2/2010 6:48 PM
Gravatar
Great work! I really enjoyed this article! Hope to read more from you soon!
传奇SF

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by fuel at 3/3/2010 10:50 PM
Gravatar
Yeah I agree 2010 will be and already is an amazing year, so much new stuff, fast tempo improvements!

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by nike air max at 3/8/2010 3:33 PM
Gravatar
how kind u r for sharing this
Nike air max

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Paul at 3/10/2010 9:47 AM
Gravatar
thanks for the leg work i was looking for this

how to control anxiety attackspanic attacks

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Grout Cleaner at 3/14/2010 2:41 AM
Gravatar
Great info, Thanks

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by Sarasota at 3/14/2010 2:43 AM
Gravatar
Excellent Visual Studio info

# re: Using WCF RIA Services without Silverlight in Visual Studio 2010 for building 3 tier ASP.NET Applications

Left by sale ed hardy at 3/17/2010 7:35 PM
Gravatar

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910