Overview
In CodeSmith Generator 6 we are upgrading the codebase to use .NET 4.0 framework.
Using the.NET 4.0 framework gives us access the Task Parallel Library (TPL).
We plan to make heave use of the Task-based Asynchronous Pattern (TAP) to provide
asynchronous call support to our API for generating templates. Microsoft describes
the pattern as follows. “The Task-based Asynchronous Pattern (TAP) is a new pattern
for asynchrony in the .NET Framework.
It is based on the Task and Task<TResult> types in the System.Threading.Tasks
namespace, which are used to represent arbitrary asynchronous operations.” Much
greater support will be added for this pattern in future version of the .NET framework,
see Microsoft Visual Studio Async CTP.
However, there is no reason we can’t make use of the pattern today.
Sample
First, a big warning that this syntax is subject to change as we evolve the code.
Here is an example of how you can use
TAP and Generator API to compile and generate a template.
Task<CompileTemplateResult> task = TemplateEngine.CompileFileAsync(path); task.ContinueWith(t => { if (t.IsFaulted) return LogError(t.Exception); if (t.IsCanceled) return LogCanceled(); CompileTemplateResult result = t.Result; CodeTemplate template = result.CreateTemplateInstance(); return template.RenderToString(); }) .ContinueWith(r => { if (r.IsCompleted) SaveOutput(r.Result); });