Code Generation & Partial Classes

I have a code generator that we use to map our database tables into objects.  The code generator works in the typical way, suppose you have an Employee table, and you want to map it to an object.  Your generator creates a file, Employee_Generated.cs, which contains all the generated data access code.  You then manually write a class, Employee.cs, that inherits from Employee_Generated, and there you can put all of your custom code for your employee object.

I’ve been updating my code generator to use partial classes and generics in whidbey, and I ran into a problem.  The generated code puts the initialization code into the default constructor, i.e., inside Employee_Generated().  This was fine when I used the inheritance model, because I could have my own default constructor in my manually coded class file.  Now, using partial classes, I can’t have my own custom constructor, because both files represent the same class! 

What I did to fix the problem, was to put my initialization code into a protected virtual method call Init(), and then I can initialize my object from different places as needed.  In hindsight, this is probably the way that I should have structured my generated classes all along, its just that I never had a problem with it until now.

Now playing: Oasis – 09 – Be Here Now.mp3

1 Comment

  1. Anonymous said,

    Wrote on March 3, 2005 @ 9:25 am

    Yeah, we’ve had to do that for ASP.NET stuff since .NET came out. VS still isn’t to the point that its generated code "disappears" like it should.

Comment RSS