Friday, November 11, 2011

Camlex.NET became bidirectional and goes online. Version 3.0 is released

Today 11.11.2011 (nice date) I have several good news. I’m happy to announce that several months of work which we made for new version of Camlex is finished. Today we released Camlex 3.0 (available on the project’s site: http://camlex.codeplex.com) with reverse engineering feature. Also we started new online service based on Camlex: http://camlex-online.org (see below).

What actually “reverse engineering” means? “Basic” Camlex.NET allows you to write CAML queries using C# code like this:

   1: string caml = Camlex.Query()
   2:         .Where(x => (string)x["Title"] == "Hello world!").ToString();

which produce the following string:

   1: <Where>
   2:   <Eq>
   3:     <FieldRef Name="Title" />
   4:     <Value Type="Text">Hello world!</Value>
   5:   </Eq>
   6: </Where>

In addition to possibility to build dynamic queries (using WhereAll(), WhereAny() methods and ExpressionHelper class) and combine expressions using logical operations (And Or) it became useful tool for Sharepoint developers and we got a many positive comments for it (thanks for everybody who posted ideas and suggestions for future development features).

With version 3.0 we reversed Camlex and taught it to work in opposite direction: from CAML to C#. It will allow to easily refactor existing code which uses hardcoded literals for CAML to the Camlex.NET with compile-time checks and code-first. In order to simplify usage for developers – we ordered domain name and created online service where you can perform this conversion: http://camlex-online.org.

So you may put CAML into the textbox:

   1: <Query>
   2:   <Where>
   3:     <Eq>
   4:       <FieldRef Name="Title" />
   5:       <Value Type="Text">Hello world!</Value>
   6:     </Eq>
   7:   </Where>
   8: </Query>

and click Convert to C#. It will produce the following code:

   1: Camlex.Query().Where(x => (string)x["Title"] == "Hello world!")

This is not the only change in 3.0 version. We extended object model so it will allow to make many other interesting conversions in future, but this is a topic of another posts.

Several words about how it works. Here is the architecture of the basic Camlex (details are here):

image

Schema of the reverse engineering is a little bit more complicated:

image

Re prefix here means Reverse Engineering. These ReTranslator, ReAnalyzer, ReLinker are completely different from Translator and Analyzer from basic Camlex. For reverse engineering we actually implemented 2nd Camlex (if we take into account amount of new code and ~500 unit tests :) ).

As you can see in output we have expression tree. In order to convert it to C# we used another open source project ExpressionToCode where we have to fix several bugs in order to adopt it for all our cases (some are reported here, but not all. Several workarounds which are specific only for Camlex were not posted). So last step is transformation from expression tree to C#:

image

Starting from version 3.0 we will implement all new features with 2 directions CAML <-> C#, so it will retain reversible. Of course reverse engineering is not complete mirror. E.g. it works only with constant CAML string, while basic Camlex supports also non-constant values (I think the reason is quite obvious). Plus it doesn’t currently supports combining of multiple And/Or operations via WhereAll/WhereAny, i.e. they will be always translated as multiple && and ||. However it is possible to add it in future.

During development of reverse engineering basic Camlex was not frozen. I created separate branch for Re version while basic CAML remain in default (fortunately after I moved it from svn to mercurial it became much easy to merge branches). When Re version was almost ready we merged default branch into re, implement reverse of the features which were added into default and merge it back to default.

As I said this release opened new opportunities for further development. Also we have plenty of feature requests which were pending until re will be finished. So we will continue our work to make Camlex better and try to simplify hard life of Sharepoint developers.

Also I would like to thanks Vladimir Timashkov who found time to help finish the work. Thanks for creators of ExpressionToCode – it greatly helped us. And as always we are open for your ideas and suggestions. Feel free to post them on the Codeplex site.

No comments:

Post a Comment