Geeks With Blogs
Greg Young Greg.ToString()

I generally avoid writing multiple posts in a day in any blog system as it aggregates but ...

 

A few days ago I made a post about partial classes being misused and the fact that they are not new and a few people came back at me discussing their use for hiding autogenerated code. I understand that this is their primary use but I challenge the thought that they are needed, does inheritance not provide the exact same end.

There may be a slight amount of higher overhead using inheritance at a level far below where you should care if you are using .NET for business applications. This overhead may not be as high as you think though, the major overhead would be caused by using a callvirt (vtable hit) instead of a call (direct) but C# defaults everything to callvirts anyways so there is no performance loss on the actual call, the main overhead would be another type in memory etc. The nice thing about using the inheritance method is that you can also isolate all of the generated classes into their own assembly and not allow changes to the entire assembly, thus the entire assembly can be regenerated.

In various discussions with evangelists etc I have been told to look at Typed Datasets to see the “need” for partial classes. In my opinion, their “need” for partial classes is a sign of a bad design. People in the object persistence world have been getting around this in their code generation for a long time I won't even comment on AOP here. I also find this need for partial classes to “hide” code from developers to be heavily influenced by the vast masses of poor developers who have come to .NET (mostly from VB 6) who don't understand the concept of design or “Dont edit this auto generated code”.

In short, I think we will be seeing alot more misuse of this functionality such as http://geekswithblogs.net/AzamSharp/archive/2005/11/23/61046.aspx as opposed to proper usage.

This could all just be my bitterness and disappointment from my first look at the “partial” keyword. I saw it and immediately started thinking AOP. I had wonderful images in my head of a dynamic proxy being implemented in the CLR and a functional weaver at compile time, in fact when I saw the word partial I had a particularly wonderful vision of C# allowing me to define a class as “partial“ meaning it was nothing more than something that could be applied as a mixin to another class but was not able to be instantiated or otherwise referenced on its own. Oh well those are (crack) pipe dreams.

 

while(3) { fork() ;}

 

Posted on Thursday, December 1, 2005 1:22 AM | Back to top


Comments on this post: Partial Classes Ctd

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Greg Young | Powered by: GeeksWithBlogs.net