D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

Partial - One instance where it helped out

Thursday, April 6, 2006 6:35 AM

Dave Chestnutt put a good post warning about the use of partial classes. I agree with him that if you're just using partial classes to break up huge classes, it might be worthwhile to re-factor your class.

I did encounter one scenario where using partial classes was very effective and helpful recently though. We recently did a project where we utilized code generation frameworks. All of our business entities were generated from the same template. However, there were cases where some of the classes needed custom methods, but they weren't required accross all entities. By using partial classes, we were able to seperate the generated code from the custom code, and made updating the project a breeze. Our file structure reflected this, so we had one set of folders for the generated code and another set to hold any custom code associated with the classes.

Inheritance in this case didn't make sense because even though all the classes were templated, they all still had different parameters, different data fields, etc. that the code gen put in...plus that would add another level of entities. With partial classes, we were able to get the best of both worlds: standards accross our classes as far as structure, with easy customization as needed without new entities being created.

So be cautious with partials, but they do have their place...as Dave warned though, make sure your design decisions are sound before implementing it.



No comments posted yet.

Post a comment