If you haven't already figured it out, we are going to use the AdventureWorks 2008 sample database from codeplex to build against. We wanted to use something that was:

A relational database model

A full scale database with real world data

Familiar to most developers

We also made sure not to build anything that required changes to the database schema or data in order to work. The purpose of this goal is that developers can plug this app into the AdventureWorks database they might already have installed.

About the AdventureWorks database

The AdventureWorks OLTP database supports standard online transaction processing scenarios for a fictitious bicycle manufacturer (Adventure Works Cycles). Scenarios include Manufacturing, Sales, Purchasing, Product Management, Contact Management, and Human Resources. AdventureWorks is used in code examples in SQL Server Books Online and in companion samples that can be downloaded from the Microsoft SQL Server Samples and Community Projects home page. The database schema is designed to showcase SQL Server features.

Building the Model

Since AdventureWorks is a large and fairly complex database, I did not want to write an entire business model for it. I chose to use Entity Framework to generate a model for me. AdventureWorks is well-structured so I figured Visual Studio would do a good job of auto-generating the model from the database schema. Let's look at how we can easily generate a Entity Framework model from an existing database.

First we need to "Add New Item" to the project and select "ADO.NET Entity Data Model".

Next we will choose to "Generate from database". We are choosing this option since the database is already well-structured and matches the model we want to work with. For highly normalized databases it is sometimes better to start with an empty model and add model views that merge tables to build useful objects.

For this application I have embedded the database inside the App_Data folder so we will choose to generate our model from the MDF file itself. I only chose this option because we will be making this application available for download and I did not want to require you all to install a separate instance of AdventureWorks on a SQL Server.

Next I will select which object in the database we want to add to the entity model. I am choosing all since I want to be able to work with Tables, Views and Stored Procedures included in the database. This dialog allows you to drill down to specific data objects in case you want to explicitly include or exclude objects (like a legacy Table).

Click finish and BOOM, we have a full object model to work with including methods for Updates, Inserts and Deletes. Below is the model we generated in minutes. As you can see, it is quite substantial and would take a long time to develop manually! Thank you ADO.NET Entity Framework for making my life easier.

Wow, that was easy.

Extending the Model

Now that we have a fairly complete object model, I want to extend it to make it more usable. The first thing I want to do is expose the data through a Web Service so we can work with the model on the client-side. I don't know what is easier: generating an ADO.NET Entity Data Model or exposing an entire ADO.NET Entity Data Model as a Web Service.

To start, I "Add New Item" and select "WCF Data Service"

Visual Studio will generate the following code for the code behind the Web Service.

We need to do two things to this code in order to get it working. First, we will specify the data source class name. The data source class name is the name you have given the ADO.NET Entity Data Model. Second, we will set access rules for the Web Service. Access rules are role-based authentication settings that allow you to create different levels of security. For this example we are going to give permission to everyone to read all data.

In just a few lines of code we are able to expose our entire model via Web service. Even if I don't immediately use the Web service in a project I still create it. You never know when you or one of your end users will need an API. Obviously, there are security measures that you need to take further than what I wrote, but it is well worth the time to be able to create a rich API for future development. If you are in a big enough organization, other dev departments might even have an immediate use for the service. So take the time and expose your data (securely).

So in just one sitting we were able to go from database to object model to Web service API. I don't care how good you are at programming, that is just cool! After some analysis I estimate these new tools in Visual Studio 2010 saving me from writing approximately 20,000 lines of code. Now that is a great way of conserving your keystrokes.