We are building a query service that needs to parse the user’s search term and generate a form of SQL (not T-SQL but a proprietary SQL-like query language) out of it.
To generate the SQL, we are looking into T4 templates.
I looked into another question here at Creating T4 templates at runtime (build-time), and I understand the basic idea; however what we need is not a physical file output but a simple in memory string containing the final SQL statement.
Is that possible?
The second question I have, which almost more important: how fast is this T4 stuff when taking into account the rather complex logic we need to generate the SQL inside a T4 template file.
Thanks.
What I think you want to take a look at preprocessed templates (aka runtime templates). You create them in visual studio 2010 by using the template Preprocess Text Template.
I created this very simple template (I named it MyTemplate.tt):
I added this partial class that extends the generated class with HowManyCommentsToGenerate field:
Finally I use it like this (note that the output is string not a file):
As far performance I honestly don’t have enough experience with run-time templates to advice you on this. I recommend reading the generated code and profiling it.