oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Build Dynamic Database Applications in .NET with Project Codename "Jasper"
Pages: 1, 2

Not Magic, Just Jasper

So, what happened in between the few lines of code you wrote and the creation of this page?

The DynamicContext control uses the Entity Framework APIs to automatically generate an Entity Data Model (EDM). The EDM is comprised of the schema files described above, and code generation is used to build a set of classes that represent the conceptual schema of your data. Each class, for example Customer or Product, is an Entity. Entity is a class in the Entity Framework's Object Services provided by the System.Data.Objects namespace. The first time Jasper builds the EDM there is a small performance hit, which is why it's a good idea to cache the context.

The AutoDataSource then uses the concept of Convention to assume that because the GridView's ID is "Products," you must want to display the Product entities. The Entity Framework then coordinates the retrieval of the data. The results of an Entity Framework query can be bound directly to any databound control. In my example, the results are bound to the GridView, which, in turn, knows how to AutoGenerate columns based on the bound data.

That's how the grid gets populated, but what about handling changes? The AutoDataSource has a property called PersistChanges which defaults to True. The Entity Framework's Object Services have change tracking built in, as well as the ability to save changes to the database. Together, the two can very easily handle updates. Again, because the updates in this sample are very conventional, Jasper does not force us to write code to make that happen.

It is also possible to bind to a column rather than an entire table. Note that the current CTP does not have the ability to sort out common column names that appear in more than one table.

Radder than RAD: A Master-Detail Page

You can turn the example created above into a master/detail view by simply adding another GridView.

By making the master GridView display Suppliers, a second Gridview can then display the Products for those suppliers. Knowing that there is a relationship between Suppliers and Products, we can assume that this will be exposed by Jasper as "Suppliers_Products." Jasper uses this construct for all relationships. By using this as the ID of the second GridView, Jasper, together with the AutoDataSource, can automatically create the Master Detail view displayed in Figure 3.

Master Detail view
Figure 3. Master Detail view

Many web developers know the difficulty of coding a master detail form, so this is a pretty amazing feat! Again, the concept of convention comes into play here as Jasper is performing the most basic and unconventional function that can be assumed about handling data in a one-to-many relationship.

Because this is a very early look at Jasper, I wasn't bothered by the fact that deleting a record in the master grid did not cause the children to be deleted. I know that Entity Framework is very smart about referential integrity and I am sure this will get worked out in future releases.

Querying Data with Jasper

Rather than always following the convention of grabbing data based on the ID of the databound control, the DynamicContext allows you to specify the data using a Microsoft.Jasper.Query class, Entity Framework's EntitySQL syntax, or even LINQ! In these cases, the AutoDataSource control is not necessary; the results of the queries can be directly bound to the databound controls. While LINQ is not yet supported directly by Jasper, it is still possible to use LINQ on the client side as demonstrated in the CTP samples.

Jasper's Query class has methods similar to Extension Methods in LINQ including Where, First, GroupBy, OrderBy, and more. Here is an example of how you can use Jasper to return specific Customers.

Dim context As Object = GetDynamicContext()
Dim query As Query = context.Customers
query = query.Where("it.Country = 'USA'")
query = query.OrderBy("it.CompanyName")
query = query.Select("it.CustomerID, it.CompanyName, it.City")
GridView1.DataSource = query

Because Jasper is using dynamic languages, LINQ uses late binding to perform its queries. At design time, the compiler has no knowledge of the schema or structure of the data classes that it will be working with, so you will be building the LINQ queries a little blindly without the help of strong typing and Intellisense. This is the dynamic language experience, not something specific to Jasper.

Customizing Jasper

While using Jasper out-of-the-box with a database will satisfy the needs of many solutions, it is possible to get more mileage out of Jasper without having to go back to standard applications. Check the CTP for examples of defining Business Logic, renaming items in the generated model, and also how to use a pre-created Entity Data Model.

The ability to create a highly customized model, one that might be used in a variety of other applications in your enterprise, including SQL Server Reporting Services, then plug it into Jasper is another example of the bigger picture of the Entity Framework--Microsoft's evolving data access platform.

Using Your Own Database

If you plan to explore Jasper with your own databases, it's helpful to understand that each table in your database will have an entity type created whose name is the singular of the table name. An EntitySet is also created for each entity whose name is the plural of the table name. It is the EntitySet that is used for queries and for establishing the Dynamic Context. If you have a database table named "foo," you will use "foos" as the ID of a databound control.

Next Steps

There is a lot more to explore in the CTP, including building Windows Forms and the AutoBinder control, updating data and a set of Iron Python samples.

Jasper is still in the very early stages of research and development and available for you to explore and question. Microsoft definitely wants feedback. The Jasper forum is a great place to ask questions and give further feedback on the product.

With the popularity of frameworks such as Ruby on Rails, it is great to see Microsoft leveraging the strength of its Data Access platform to provide .NET developers with the ability to create dynamic applications in scenarios where even a drag-and-drop ASP.NET application seems like overkill. The addition of languages like IronRuby and Iron Python, will surely get a lot of non-Microsoft developers to take a look at using Jasper to build their web applications.

Julia Lerman is the leading independent authority on the Entity Framework and has been using and teaching the technology since its inception two years ago. She is well known in the .NET community as a Microsoft MVP, ASPInsider and INETA Speaker. She is a prolific blogger, a frequent presenter at technical conferences around the world, including DevConnections and TechEd and she writes articles for many well-known technical publications.

Return to