Camlex.NET 3.0: Reverse Engineering and Online Version
In November 2011, we released the next major version of our open source library Camlex.Net 3.0. We also launched the online version based on Camlex.Net 3.0: http://camlex-online.org. In this article, I will describe the new features and architecture of the new version.
The original "old" Camlex allows developers to create CAML queries without working with actual CAML, i.e. if you need to fetch all items which Title equal to "Hello world!
", we can do it using the following code:
string caml = Camlex.Query()
.Where(x => (string)x["Title"] == "Hello world!").ToString();
which will produce the following string
:
<Where>
<Eq>
<FieldRef Name="Title" />
<Value Type="Text">Hello world!</Value>
</Eq>
</Where>
Also, you can combine expressions using && (AND) and || (OR) operations to build complex CAML queries dynamically.
If you would like to know more about other features of previous version of Camlex, you can check the following articles on CodeProject:
Starting from version 3.0, it also supports opposite conversion: now you can pass CAML query as string and it will produce C# code which can be used instead of hardcoded string in your projects. It simplifies refactoring of existing applications to the Camlex.Net.
But this is not all. The nice thing is that you don't have to download Camlex assembly and create an application with it if you need to convert CAML to C#. We also launched the online version http://camlex-online.org where you can put CAML queries and convert them to C#. If you will try to convert CAML shown above, it will produce the following code (in online version, additional Query
tag is required as parent tag for all queries):
Camlex.Query().Where(x => (string)x["Title"] == "Hello world!")
As you can see, it produced the same code as we wrote above, but now Camlex made it for us! We called this feature Reverse engineering as all CAML operations which are implemented in Camlex.Net (all CAML elements except those which were introduced with Sharepoint 2010, e.g. In and Includes) were reverted to opposite direction: from CAML to C#.
Technically, we implemented 2nd Camlex in opposite direction. The following picture shows the architecture of "basic" Camlex:
There is C# lambda expression on the input and CAML query on the output. In order to convert lambda to CAML, we implemented translator and analyzer. For reverse engineering, we have CAML on the input and C# on the output. So we implemented another translator and analyzer plus linker which links different parts of the query together (CAML query is not limited by only Query element, but also ViewFields, OrderBy, GroupBy). So the reverse engineering architecture looks like this:
(Re prefix means Reverse Engineering). As I mentioned, technically we implemented 2nd Camlex. Amount of unit tests is a good indicator here: it was increased in ~2 times (~500 tests currently).
As it's shown on the picture, there is an expression tree on the output. It is possible to convert expression tree to string, but it won't be compilable C# code. In order to print C# code in the online version, we used a great open source project: ExpressionToCode. It also has a translator which converts expression trees to C# code:
After combining these technologies (several issues in Expression2Code were fixed for this. Most of them were reported in the issues section of ExpressionToCode site. Only several Camlex-specific workarounds were not posted), we launched online converter.
Reverse engineering version will make development even more faster. You can create CAML query using U2U CAML Builder tool and convert it to Camlex using online service. So you will have advantages of both tools: interactive CAML queries creation and compile-time checks.
As I mentioned above, we revered all operations which are supported in the Camlex at the current moment. E.g. if you will enter CAML which contains OrderBy
:
<Where>
<Eq>
<FieldRef Name="Title" />
<Value Type="Text">Hello world!</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name="Modified" />
<FieldRef Name="Modified By" />
</OrderBy>
it will be converted to the following C# code including fluent interfaces calls:
Camlex.Query().Where(x => (string)x["Title"] == "Hello world!")
.OrderBy(x => new[] { x["Modified"], x["Modified By"] })
In the future also, each new feature which will be added to Camlex will be added with 2 directions: C# to CAML and CAML to expression tree, i.e., we will keep Camlex reversible.
You can download the source code and binaries directly from CodeProject, or from CodePlex. We will appreciate your feedback for the Camlex, which you can post here.