Using Dynamic Data in Visual Studio 2010

VS2010 Dynamic Data templates eliminate tedious coding in data-driven applications

Alvin Bruney

January 27, 2010

4 Min Read
ITPro Today logo


Click here to download the sample database for this article.

Part 2 of Alvin Bruney's Dynamic Data in Visual Studio 2010 series.

If you've ever built data-driven applications (and who hasn't), you understand the enormous effort required to make the application production ready. For instance, there are update statements to write, validation logic to implement and cosmetic treatments to apply to each User Interface (UI) widget. After you've built a few of these applications, you'll inevitably begin searching for a generic way to handle the manual labor. You'll likely come up short and decide to build this yourself.

But, there's a better way! Dynamic Data in Visual Studio 2010 is designed to handle the tedium for you. It essentially creates the plumbing code that services data-driven applications so you won't have to. Let's take this for a spin. Create a project based on a Dynamic Data template, see Figure 1. Name the project DDExample1.

Note there are two Dynamic Data templates. You should prefer the use of the Dynamic Data Entities Web Application template; Microsoft is investing significantly in that direction. Click Ok to generate the Visual Studio project. Figure 2 shows the newly created Dynamic Data Entities Web Application. Let's add a database to the project by right-clicking the App_Code folder and selecting Add, Existing Item. You can download the sample database to follow along. The sample database gives us some mock data to play with. Or, you can use any available database.

Now that the data store is available, I'll add a model abstraction on top of the data store. That will setup the infrastructure to allow me to begin using domain objects. Select the DDExample1 active project, right-click and choose Add, New Item. Type the word Model in the Add New Item dialog's filter box, as you can see in Figure 3. Select the ADO.NET Entity Data Model. Enter DDModel.edmx in the Name field, as shown in Figure 3. Note: The filter option was a key customer request and is new for VS2010. It simplifies the process of finding and using available templates.

Click Add to have VS2010 add the model to the application. Note that the Entity Model is added to the project as a whole. The option does not appear if you select a sub-folder in the active project.  The Entity Data Model Wizard launches, as Figure 4 shows.


Walk through the wizard accepting the defaults. Since I've added the database first, the wizard is smart enough to attach to it. Make the selections shown in Figure 5.

Also make sure that the name of the entity is KingOfPopModel; that will come in handy later. Click Finish. The infrastructure hook-up is complete. In Solution Explorer, open the Global.asax file. Find and uncomment the line similar to this:

 

DefaultModel.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() \{ ScaffoldAllTables = false \});

 To enable dynamic data to work with the new model, set the ScaffoldAllTables to true. You'll need to scaffold at least one table otherwise a run-time exception will be thrown. Also, pass in the entity model to the RegisterContext method. That will register and expose the context object to the code so that I can work with strongly typed domain objects. The completed code looks like this:

 

DefaultModel.RegisterContext(typeof(KingOfPopEntities), new ContextConfiguration() \{ ScaffoldAllTables = true \});

 Press F5 to run the application, see Figure 6.

The web application is production ready. The application can:

  • Page, sort, insert, and delete data.

  • Select and edit item data.

  • Provide a detail view for selected data.

  • Adjust the size of data in the page.

  • Confirm entity delete operations.

  • Validate against the entity model.

All that functionality came for free; I did not have to write any code, well one line to be exact. Much of the magic driving this application is based on the intimate knowledge that the entity model has of the data store. The model can translate domain concepts into efficient database queries.

Dynamic data also supports AJAX. I can quickly add AJAX support by setting the EnablePartialRendering method of the ScriptManager object to true. The ScriptManager object is located in the site.master master page.

Dynamic data does most of the heavy lifting using templates. Templates can be customized, modified, and extended to create powerful data-driven applications. These applications can be built and deployed in the time it takes to finish off that morning coffee. You can even inject Dynamic Data infrastructure into ordinary web projects!  In the next article, I'll walk you through complex validations and customizations so that you can make your web applications sing!

Alvin Bruney is a longtime Microsoft MVP and author. His new book, ASP.NET 4 by Example is available on www.lulu.com/owc.

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like