Skip to content

Using result transformers

rosaliafx edited this page Apr 5, 2015 · 2 revisions

Let's consider the task that fetches version number from svn:

ITaskFuture<SvnVersionResult> getVersionTask = Task(
    "svnVersion",
    new SvnVersionTask
    {
        /* ... */
    });

This task returns SvnVersionResult that contains Min and Max revisions etc, but our goal is to compose a regular string version of product like "1.0.43". To accomplish it, we could introduce another task that transforms the SvnVersionResult to string:

var getVersionTask = Task(
    "svnVersion",
    new SvnVersionTask
    {
        /* ... */
    });

var formatVersionTask = Task(
    "formatVersion",
    from svnVersion in getVersionTask
    select string.Format("1.0.{0}.0", svnVersion.Max.Number).AsTaskResult());

It will work, but the code is too verbose and we have a redundant task. As an alternative, We can use TransformWith helper, that accepts a transformation delegate and wraps original task with transformation logic:

ITaskFuture<string> getVersionTask = Task(
    "svnVersion",
    new SvnVersionTask
    {
        /* ... */
    }.TransformWith(svnVersion => 
        string.Format("1.0.{0}.0", svnVersion.Max.Number)));