Posted by Igor Moochnick on 08/20/2012
The brute force solution of using the T4 templating engine to emit both the client-side and the server side data models didn’t look that appealing to me. Primarily due to a simple fact that it’s not that easy to refactor the T4 templates. And, the ReSharper junkie like me, can’t tolerate this fact.
Note: all of the code is an intermediate solution that was answering my personal needs for the project I was running. It is in no-way represent the full fledge solution. If you want something bigger and more feature-reach, you can either contribute to my solution or look into projects like Script# or SharpKit.
The workflow, I ended up with, was very simple:
- Build a web application
(*) – I’ve decided to use attribute as an option to reduce unnecessary noise from the compilation. The compiler will ignore all of the non-marked classes. This is not a requirement, but an option.
The simple C# data model that I needed for the client-server communication looks like this:
#> jinxc [original] [destination]
The generated model is very simple (in the current implementation) and represents the RAW structure that you need to fill in before sending it across the wire. JQuery ajax helpers are very handy for that:
As you can see, the usage of the Jinx compiler is straight forward, but the underlying “magic” is not that simple. Main task is handled by Roslyn itself – it parses the source C# and generates a full AST (abstract syntax tree).
There are 2 main ways to traverse the tree:
- Use the SyntaxWalker helper class – override only the handlers of interest (i.e. “override void VisitVariableDeclaration” will be called ONLY when a variable declaration is visited)
- navigate through all of the nodes and their properties by accessing the AST nodes directly
The Jinx implementation uses both techniques to achieve its goals: first one – to jump directly to the points of interest and the second one to interrogate the context in details.
To build the Jinx compiler you’ll need the following Prerequisites:
I’ll be writing more about Roslyn in general and Jinx in particular. So stay tuned …