I wrote about the couple new projects that was announced by Microsoft recently and I’ve installed “Jasper” and I had some free time this weekend to play with it.
You may ask What is Jasper? The answer comes from documentation:
Microsoft® Codename “Jasper” is a set of components aimed at fulfilling the need for a rapid and iterative development experience for data. With “Jasper”, you are able to just point at a database and immediately begin coding against its data using intuitive, domain-specific data classes. No configuration and no source code generation are required. Jasper works with existing application frameworks (including ASP.NET, WinForms, and WPF) and existing, real-world databases.
First thing I noticed is the first CTP comes with samples in Visual Basic and IronPython which is not the best way to impress C# fans J I haven’t given up and I kept investigating and I am going to share with you my findings.
Working with Jasper is very similar to LINQ to SQL. Everything is done through DynamicContext class and using it’s methods GetQuery, AcceptAllChanges, CreateQuery and many others . The difference is that in LINQ to SQL there is a class that extend DataContext class but in Jasper DynamicContext class is compiled in MS assemblies. If you play with it you’ll notice there aren’t any properties that represents tables in underlying database. But this is code snippet from samples that comes with Jasper:
Dim connectionString As String
Dim context As Object
connectionString = _ ConfigurationManager.ConnectionStrings("Northwind").ConnectionString
context = DynamicContext.CreateDynamicContext(connectionString)
'*** get Customer table by late binding
Dim query As Query = context.Customers
'*** bind to grid
ResultsGrid.DataSource = query
ResultsGrid.DataBind()
Do you see the row that ends with context.Customers? On this row the content of Customers table is got and can be used in lines below to be bound to GridView control. As we said the class DynamicContext is same for all applications and the tables are got using late binding because the Jasper data classes are generated dynamically at runtime when DynamicContext.CreateDynamicContext() is called.
Late binding could be painful in C# and it seems this is reason not having samples in C# for nowJ.
I will cover another feature in Jasper – dynamic queries. There is very easy way to construct queries in Jasper. All you have to do is using Microsoft.Jasper.Query class and it’s methods Select, OrderBy, Where, Union ( and many others ):
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")
Dynamic queries are doubtful from architecture standpoint as they could harm the design and easy mess DAL code with business logic. This is why I recommend avoiding dynamic queries when possible or use them very carefully.
This blog entry contains very little code but I can assure you this is enough to get Customer table content and display it in Grid control on ASP.NET page. J
You can get Jasper from Microsoft Downloads.
And don’t forget the prerequisites:
1) Microsoft Visual Studio® Codename “Orcas” Beta 1. Install details can be found at http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx.
2) Microsoft SQL Server™ 2005. The Microsoft SQL Server 2005 Express Edition can be found at http://www.microsoft.com/downloads/details.aspx?familyid=220549b5-0b07-4448-8848-dcc397514b41&displaylang=en.
3) (Optional) Microsoft Iron Python 1.1. The install can be found at http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=2573.