Code Generation and Visual Studio 2003

I've had a chance recently to speak with people about Code Generation and problems they perceived using Visual Studio 2003.  Everyone is quick to point out that partial classes will solve alot of the problems with Code Generation when we have Visual Studio 2005, but they find it tedious or difficult to extend classes they have generated and then late regenerate them in Visual Studio 2003.  Still, there are several perfectly good options for this in VS03.

  1. Generate abstract classes which you then inherit from.  Particularly useful for data access layers, this would produce an AbstractTableName and then a simply 3 line TableName class. For example, if your table was named Customer and you were writing in VB, you would have AbstractCustomer.vb which looks like:

    'This is a class generated by your favorite Code Generation utility

    'such as CodeSmith (www.codesmithtools.com)

    Public MustInherit Class AbstractCustomers

        'Generated properties and code go here...

    End Class

    And a Customer.vb file which you did not generate which started containing only:

    Public Class Customers

        Inherits AbstractCustomers

    End Class

  2. Another valid option is to use preservation regions, a feature in my favorite code generation utility CodeSmith.  This allows you to create a region which will be left, untouched, after you regenerate the file

Certainly we will have an easier time once we have partial classes, but as it will take time for shops to upgrade, you can call these techniques when you have to generate code and then extend upon the generation.