Build Dynamic Database Applications in .NET with Project Codename "Jasper"by Julia Lerman
Dynamic programming has become more popular as developers look for simpler ways to do Rapid Application Development (RAD) of database applications. Ruby on Rails is a great example of this for web applications, and its rise in popularity is proof of its value to developers. But instead of moving to Ruby on Rails, ASP.NET developers might consider Project Codename "Jasper" and the growing number of .NET dynamic languages, including Iron Python, IronRuby, and Visual Basic.
Project Codename "Jasper" is a new incubation project for building dynamic applications---programs that practically create themselves on the fly--that was unveiled at MIX07 in Las Vegas this past spring by Microsoft's Data Programmability team.
Jasper and technologies such as Rails favor "Convention over Configuration." The idea behind this concept is that a developer should only have to write code for behavior in an application that is "unconventional." With databases and other data stores, a lot of assumptions can be made based on their metadata and schemas. For example, a program might assume that if an Employees table has the fields FirstName, LastName, StartDate, and an Auto-Incremented Primary Key field of ID, then a form could be created to add, edit, or delete an individual row using the Primary Key to synch modifications to the database. If there is a Primary Key/Foreign Key relationship between that table and another, then the program would create a master/detail form between the two tables. However, if you wanted to do something different, such as not include StartDate in the data entry form, or pull in data from related tables, that would be considered "unconventional". The only code you should need to write is for behavior that does not follow "conventional" assumptions.
One of the features of the existing frameworks, such as Ruby on Rails and Django, is that they rely on the schema of the actual data store (such as a database or XML files), which they can inspect and then use to define forms and reports. Often, these storage schemas are structured to support efficiency and security, rather than organizing data so that it aligns nicely with the developers' business model.
Jasper and the ADO.NET Entity Framework
What makes Jasper different from these other web application frameworks is that, rather than depending directly on the database for its schema, it uses an Entity Data Model created by the ADO.NET Entity Framework.
The Entity Framework is the core of Microsoft's evolving data access platform. It is currently in Beta and will be released in the first half of 2008 as an out-of-band set of APIs for Visual Studio 2008. The Entity Framework codifies the use of Entity Relationship Models, which have, for the past 30 years, lived mostly on whiteboards. These models can be designed to represent a data schema in a way that is more appealing to developers without impacting the database. APIs then allow a developer to write code against his conceptual model, while mapping and data store schemas are used to coordinate the movement of data back and forth to the database. This is not the same as ORM tools because the Entity Framework does not create a tight binding between business objects and the database. The Entity Data Model is a layer of abstraction in between objects and the database or some cases, in between a presentation layer and the database. Entities only represent data schema. Unlike objects, they do not define behavior.
Jasper's use of the Entity Data Model instead of connecting directly to the database means that there will be fewer anomalies to manually code around. For example, if the database was normalized so that contact details are spread across a number of tables (contacts, emails, telephones, and addresses) you could customize a model so that all of this information is contained in one entity. Then when you create a dynamic application, a detailed contact record will be created automatically and not require custom code. Much more of the application can be truly dynamic.
Downloading the Jasper Preview
A Community Technical Preview (CTP) of Jasper has been available from Microsoft a CTP since its release at MIX07. The Jasper CTP comes with a few dozen examples and can be downloaded here. Be sure to read the overview document for additional installation requirements and instructions.
Because of the dynamic nature of Jasper, it is necessary to use a dynamic language; Microsoft has been busy creating some of those as well and, of course, there's Visual Basic.NET (VB10 will be a dynamic language). Half of the samples in the CTP use Visual Basic.NET, while the other set of samples use the Iron Python language. Microsoft's new Ruby implementation, IronRuby, should work with Jasper in a future release, but was not ready for this CTP release. Because C# and other static languages perform type checking, they cannot be used with Jasper.
A Jasper Web Application
Let's take a quick look at a data-centric web application that uses Visual Basic and Jasper.
There are very few steps involved. First you will create an editable grid view of product data, then, with a few tweaks, you can turn the grid view into an editable master/detail view. While you can just read through the steps to get the concept of how you go about creating a web application using Jasper, if you wish to build this demo, you'll need to install the Visual Studio 2008 Beta1 and the Jasper CTP as described in the previous section.
Create a web page like the one shown in Figure 1 by dragging on to it both an ASP.NET databound control (such as a GridView) and a Microsoft.Jasper.AutoDataSource control from the Visual Studio toolbox. You can add the AutoDataSource control to the Toolbox by selecting Program Files\Microsoft Codename Jasper CTP\Binaries in the Toolbox wizard.
Setting the ID of the GridView to "Products" tells the AutoDataSource all it needs to know. This property will be used to populate the GridView.
Figure 1 shows what the completed page should look like in design view.
Figure 1. Web page with GridView and AutoDataSource
In the code-behind, you will need to create a Microsoft.Jasper.DynamicContext. The DynamicContext is an object that will track any changes the user makes to the data and also coordinate sending those changes back to the database. DynamicContext takes a database connection string as a parameter. The Jasper samples use a connection string in web.config that is set to point to the Northwind database (included in the CTP). The connection string has more information than you will see in a typical application. These additional parameters are required for Entity Framework and Jasper. Check the Jasper documentation for more information about the connection string. The GetDynamicContext method below checks for the existence of the context in SessionState and creates it, if necessary. Note that in the current CTP, DynamicContext is not thread-safe.
Private Function GetDynamicContext() As DynamicContext Dim context As DynamicContext = Session("DynamicContext") If context Is Nothing Then Dim connectionString As String = "Provider='System.Data.SqlClient';Provider Connection String='Data Source=.\SQLExpress;Initial Catalog=Northwind;Integrated Security=True;';Generate Default EDM=True;" context = DynamicContext.CreateDynamicContext(connectionString) Session("DynamicContext") = context End If Return context End Function
The last step is to assign the DynamicContext to the AutoDataSource control on your web page. The AutoDataSource control orchestrates the binding of the data that is provided through the DynamicContext to the GridView. This is another example of how Jasper will use assumptions to do its job. AutoDataSource knows to look for a databound grid. It will see the GridView and assume that this must be the control to send the data to. Here I have implemented the assignment right in the
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load AutoDataSource1.DynamicContext = GetDynamicContext() End Sub
Believe it or not, that's all that is required to render the page shown in Figure 2 with data that can be updated.
Figure 2. Dynamically created web page
Pages: 1, 2