Galin Iliev's blog

Software Architecture & Development

Hello "Jasper"

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.

Comments (2) -

  • sayantika

    6/20/2007 5:08:42 AM | Reply

    hi,
    i've started working on JASPER .. but been encountering with many obstacles with no one to guide me around. so i needed help....
    if we use a join of two table in our sql query where both tables have columns with the same name... how do u we resolve the conflict between the names during adding a field...
    any help would be deeply appreciated

  • Galcho

    6/21/2007 8:35:09 PM | Reply

    I suppose that precising query and give unique column names would be enough. As Jasper gets the query this should solve the problem

    the query would look in similar way
    SELECT t1.Id, t1.[Name], t2.Id as Table2Id, t2.[Name] as Table2Name
    FROM Table1 as t1 INNER JOIN Table2 as t2  on t1.Id = t2.RelationId

    this will return you 4 columns: Id, Name, Table2Id, Table2Name

    Hope this helps

Loading