Code Generation

I am a huge fan of code generation.  I've used it to incredible advantage.  I'm talking about real dollar value impacting effect that could make or break a project.  If you're reading this and haven't yet drunk the Kool-Aid, then now is the time.  Read on. Code Generation is the process of letting code write code, usually at design-time.  There are tons of examples on the net to demonstrate this and I won't repeat them here (follow some links below and you'll get to some fine examples).  Instead I'm going to discuss the concept pure theoretically. When to "CodeGen" : The criteria for this are quite simple.  If you have something which already defines the structure you desire and you are trying to reflect that same structure in something else, then you should use code generation.  This is why data access layers are the classic example, because you are mimicking the structure of the database in your data access layer. Why to "CodeGen" : Time and Agility!  The time is takes to setup a code-generation system is a fragment of the amount of time it takes to write the same code for all source entities.  Your template takes slightly longer than the time it takes for one class, but can then be instantly be applied to all your source entities.  If you have a database with 50 tables, write one class, turn that one class into a template and bingo ... 50 classes. Agility is the other reason.  Let's say you code all 50 classes by hand, and then the next day the DBA refactors your database and adds a new table which has foreign keys to lots of other tables.  Your 50 classes, pretty much toast.  If you have generated those 50 classes using code generation, then this is no problem, simply re-run your same template (no changes there, it's agnostic to what database it's looking at) and bingo, 51 classes all with the right relationships. What to "CodeGen" : Data Access Layers are popular, but so are lots of other things.  Databases make a good structured source of entities you could generate against, but so are XSD Schemas, XML Files, and lots of other stuff. Where to "CodeGen" : There are lots of great tools to use for code generation out there, so many I could likely exhaust my fingers typing them all, but there are just a few which come with my "Stamp of Approval": CodeSmith - From CodeSmith Tools this is, in my opinion, the greatest code generator going.  I use it for almost all my code generation.  The syntax is very much like ASP.NET and is easy to manage.  The product is also easy to integrate into a nightly build process. .NET Tiers - This is a set of templates create by the community for use with CodeSmith.  These templates will generate stored procedures, data access layers, business objects and web controls all from your database at the push of a button.  Oh, and it all has Unit Tests.  Check this out. XSDObjectGen - One of the few things CodeSmith does not do well, yet, is generate from XSDs.  XSDObjectGen creates Serializable classes which represent your XSD's structure completely.  XSD.exe, which comes with Visual Studio, does similar things, but XSDObjectGen does it 10x better.

TimRayburn.CustomFunctoids - Logical XOR

This functoid does not take a lot of explanation, it simply fills another gap in the functoid library.  For those who may not be familiar with the glories of XOR, allow me to present the truth table for it. As you can see, the difference between this operator and "Or" is that when both inputs are true, it returns false.  This is very nice when you need to handle edge cases where you could use either x or y but want to do something different when both are present. The code, as always, is included below.  I'm trying Scott Dunn's Windows Live Writer Code Formatter on this post for the first time.  If there are problems, let me know and I'll go back to CopySourceAsHtml which I have been using.  Scratch that, it just blew up as I tried to publish this post, so we're back to using CopySourceAsHtml. Download TimRayburn.CustomFunctoids v1.0      13         public LogicalXorFunctoid()   14             : base()   15         {   16             // Assign a "unique" id to this functiod.   17             this.ID = 24605;   18    19             // Setup the resource assembly to use.   20             SetupResourceAssembly(   21               "TimRayburn.CustomFunctoids.CustomFunctoidsResources",   22               Assembly.GetExecutingAssembly());   23    24             SetName("IDS_LOGICALXORFUNCTOID_NAME");   25             SetTooltip("IDS_LOGICALXORFUNCTOID_TOOLTIP");   26             SetDescription("IDS_LOGICALXORFUNCTOID_DESCRIPTION");   27             SetBitmap("IDS_LOGICALXORFUNCTOID_BITMAP");   28    29             this.SetMinParams(2);   30             this.SetMaxParams(2);   31    32             //set the function name that needs to be    33             //called when this Functoid is invoked. This means that   34             //this Functoid assembly need to be present in GAC    35             //for its availability during Test..Map and Runtime.   36             SetExternalFunctionName(this.GetType().Assembly.FullName,   37               "TimRayburn.CustomFunctoids.LogicalXorFunctoid",   38               "LogicalXor");   39    40             this.Category = FunctoidCategory.Logical;   41             this.OutputConnectionType = ConnectionType.AllExceptRecord;   42    43             AddInputConnectionType(ConnectionType.AllExceptRecord);   44         }   45    46         public string LogicalXor(string firstVal, string secondVal)   47         {   48             bool bFirst = System.Convert.ToBoolean(firstVal);   49             bool bSecond = System.Convert.ToBoolean(secondVal);   50             bool result = bFirst ^ bSecond;   51    52             if (result)   53               return "true";   54             else   55               return "false";   56         }   57     }

SOA & Business Process Conference

This week Microsoft is hosting the SOA & Business Process Conference in Redmond, and obviously BizTalk Server is right at the heart of it all.  You can follow all the fun over at the BizTalk Server Team Blog.  I don't get to go this year, but I'm hoping to next year, as well as TechEd 07.