TimRayburn.CustomFunctoids - If ... Else

This post is part of an ongoing series, the explanation of which can be found here.

This one should be of more use to our friends using BizTalk Server 2006.  Today we tackle a problem that anyone who has ever mapped an EDI 837 I/P/D has had to deal with, and probably solved with hundreds of "Value Mapping" functoids and Logical Not (if you are on 2006) functoids.

The out of the box functoid set gives you a very nice "Value Mapping" functoid which is great because it compiles to an xsl:if tag.  But if you want to do a simple If A then B else C type logic you are left with either two value map functoids and a Logical Not (or Logical Equal in 2004) functoids, or you are writing a scripting functoid.  Either way you have hampered the readability of your map, and in the case of the value map solution you've netted yourself a compiler warning for two links to one node for each and every node you had to do this with.  If it is an 837 that you're doing this with, that is a TON of nodes in the 2300 and 2400 loops.

The solution?  IfElseFunctiod of course!  This class, added to the TimRayburn.CustomFunctoids project, takes three parameters.  The first is a boolean, the second is the value returned if true, the third is the value returned if false.  It is important to note that in any case, if connected directly to the output schema this will produce a node.  It is not a XSLT functoid and cannot suppress the output node.  For now if you need to do that, you're still stuck with putting in a Value Mapping functoid.

The newly added class can be found below.  It is nearly identical to last night's functoid of course.  Interestingly, I tried to refactor this class and the other to create a base class that handled assigning the ID and resources.  When I did so, BizTalk stopped recognizing that the assembly contained any functoids.  Apparently your custom functoids must not simply inherit from BaseFunctoid, they must be direct children of BaseFunctoid.

Download v1.0 of TimRayburn.CustomFunctoids

   10     class IfElseFunctoid : BaseFunctoid
   11     {
   12         public IfElseFunctoid()
   13         {
   14             // Assign a "unique" id to this functiod
   15             this.ID = 24602;
   17             // Setup the resource assembly to use.
   18             SetupResourceAssembly(
   19               "TimRayburn.CustomFunctoids.CustomFunctoidsResources",
   20               Assembly.GetExecutingAssembly());
   22             SetName("IDS_IFELSEFUNCTOID_NAME");
   23             SetTooltip("IDS_IFELSEFUNCTOID_TOOLTIP");
   24             SetDescription("IDS_IFELSEFUNCTOID_DESCRIPTION");
   25             SetBitmap("IDB_IFELSEFUNCTOID_BITMAP");
   27             this.SetMinParams(3);
   28             this.SetMaxParams(3);
   30             SetExternalFunctionName(this.GetType().Assembly.FullName,
   31               "TimRayburn.CustomFunctoids.IfElseFunctoid",
   32               "IfElse");
   34             this.Category = FunctoidCategory.ValueMapping;
   35             this.OutputConnectionType = ConnectionType.AllExceptRecord;
   37             AddInputConnectionType(ConnectionType.AllExceptRecord);
   38             AddInputConnectionType(ConnectionType.AllExceptRecord);
   39             AddInputConnectionType(ConnectionType.AllExceptRecord);
   40         }
   42         public string IfElse(string booleanValue, string trueValue, string falseValue)
   43         {
   44             bool bVal = System.Convert.ToBoolean(booleanValue);
   45             if (bVal)
   46               return trueValue;
   47             else
   48               return falseValue;
   49         }
   50     }