"Active Code Generation" for CSLA

"Active Code Generation" for CSLA

Old forum URL: forums.lhotka.net/forums/t/3966.aspx


rhinoishere posted on Friday, November 30, 2007

I want to add a Pre-build command-line event to Visual Studio so that my business objects are rebuilt based upon the current database schema. 

Yeah, I know the argument against this (makes your apps data-centric), but we already have to many business objects tied directly to the backend tables. So some active code gen could save me a lot of time.

Does anyone currently do this, or can you provide me a link with some example code?  Looking at using either CodeSmith (don't mind paying if I have to) or MyGeneration.

thanks, Ryan

Q Johnson replied on Friday, November 30, 2007

If you're doing your generation with .NET 2.0 or later, you should be taking advantage of parital classes.  You can protect your hand-crafted code in one file and use the other for generation.  Then you can generate with impunity whenever you want with whatever tool you want.

If you aren't using partial classes, I'd guess the relatively sophisticated generators (the two you mention should certainly qualify here) have techniques for handling the protection of hand-crafted code during generation.

I don't know CodeSmith well enough to comment on it.

But MyGeneration has a facility that handles this pretty well.  You define the names that mark you hand-crafted code regions.  At generation time, the engine visits all the markers and saves the code it finds at them and then spews it into the the generated file when it comes to the markers during output.

But partial classes is MUCH easier.  FYI, the MyGeneration website's forum has instructions for putting a call to MyGeneration's command-line utility in your VS external tools.  And you can batch several templates to be processed with one call.  Check out the forum's Tips and Tricks section.

 

HTH

rhinoishere replied on Tuesday, December 04, 2007

Thanks for your reply.  I tinkered around with MyGeneration for an hour or two after going to the Tips and Tricks section per your suggestion. While I was able to generate CSLA code with it, I wasn't able to get any sort of active generation working. So as always seems to happen, I end up back at CodeSmith and in about an hours time I had a working active generation solution.  The way CodeSmith integrates into Visual Studio and the build process is very clean, very nice.  So, I'm probably just going to go with CodeSmith.  While I obviously like that MyGeneration is free, I'm getting lazy in my old age (getting to mid-thirties now, GASP!) and it looks like CodeSmith has done a lot of the work for me.

And yes, I 'm loving the partial classes.  Pefect solution for code generation.  Man, I've always kind of stayed away from code generators but now that I've taken the plunge I'm really seeing their power. Especially when used for active generation. Seems to me that if you are using a code generator then active generation is the next logical step, but that's probably another topic.

thanks again, Ryan

Copyright (c) Marimer LLC