Code generation has been the driving force behind partial classes. The need is based on the fact that the class generated by the code is constantly changing, but allows developers to provide their own code as part of the class, which will not be redefined every time there are changes that cause the class to be restored.
Take WinForms or Typed-DataSets, for example (or any designer, for that matter). Each time you make changes to the constructor, it serializes the corresponding code to the file. Let's say you need to provide some additional methods that the generator knows nothing about. If you added it to the generated file, your changes will be lost the next time you create it.
The project I'm working on uses code generation for all DALs, BLLs, and business objects. However, the generator receives only 75% of the information. The rest should be hand-encoded (e.g., custom business logic). I can assume that every BLL class has a SelectAll method, so it's easy to generate. However, my BLL client should also have a SelectAllByLocation method. I cannot put this in my generator because it is not common to all BLL classes. Therefore, I generate all my classes as partial classes, and then in a separate file I define my own methods. Now along the way, when my structure is changing, or I need to restore my BLL for some reason, my user code will not be destroyed.
Micah Oct 02 '08 at 2:33 2008-10-02 02:33
source share