The Module, a new Design Pattern ? 5

Posted by jbevain Mon, 25 Apr 2005 13:31:04 GMT


I’m partially enthusiast about the partial classes. Here is a sample of code that a French Microsoftee presented to us :

<!- code formatted by http://manoli.net/csharpformat/ ->
<span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Person
{
    <span class="kwrd">public</span> <span class="kwrd">string</span> firstname;
    <span class="kwrd">public</span> <span class="kwrd">string</span> lastname;

    <span class="kwrd">public</span> Person (<span class="kwrd">string</span> firstname, <span class="kwrd">string</span> lastname)
    {
        <span class="kwrd">this</span>.firstname = firstname;
        <span class="kwrd">this</span>.lastname = lastname;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Format ()
    {
        <span class="rem">// ...</span>
    }
}

<span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> Person
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> Person FromDataRow (DataRow r)
    {
        <span class="rem">// ...</span>
    }

    <span class="kwrd">public</span> <span class="kwrd">static</span> Person FromAnotherDataSource (DS s)
    {
        <span class="rem">// ..</span>
    }
}

Isn’t it a neat design ? Grrrr, I know this is only a sample, to explain the partial keyword, but i know that lots of people will use it this way. Please, use only the partial keyword where it is meant to be.

Trackbacks

Use the following link to trackback from your own site:
http://www.evain.net/blog/articles/trackback/16

Comments

Leave a response

  1. Avatar
    Christian Gross Mon, 25 Apr 2005 17:33:45 GMT

    Funny that you were thinking this. Today I started a chapter where I go into this material and thinking that I am not a fan of partial classes. EXACTLY for the reason you showed. I call the keyword partial, the MPD (Multiple Personality Disease) problem.

  2. Avatar
    MatzeB Tue, 26 Apr 2005 10:18:21 GMT

    Doesn’t aspect oriented programming do the same thing (plus more)? Also I wonder how a good example looks if this is a bad example…

  3. Avatar
    Bertrand Le Roy Tue, 26 Apr 2005 20:54:43 GMT

    Bad, bad, bad example. It’s not because partial classes are new that you should use them.
    Partial classes were introduced for only one reason: making it easier for design tools. Thanks to partial classes, for example, you don’t have to redefine your web controls in your code behind, the markup in the aspx is enough.
    But that example doesn’t make sense.

  4. Avatar
    mawi Fri, 29 Apr 2005 13:07:49 GMT

    I too have not seen this type of example of partial keyword usage, yet I too (like MatzeB above) immediately thought of AOP.

    If you look at it from an AOP viewpoint I think it makes more “sense”. In a way, I think you could actually use the partial keyword somewhat like a joinpoint enabling wrapper advice on a class level.

    There are advantages to viewing persistence as a crosscutting concern, which is essentially what the example does.

    Is it good? I cant decide, I have not gotten enough experience with either partial or AOP to have formed an opinion.

  5. Avatar
    Jb Evain Fri, 29 Apr 2005 17:27:41 GMT

    Mawi, MatzeB,

    I strongly disagree with the idea that the partial keyword can be used to implement a crosscuting concern. In fact, every pieces of the partial class should share the same concern. It is just here to simplify the readability of the source, when a bunch of the source if generated. In my opinion of course …

    Using the partial keyword, both concerns are really close, it is the same class !!! Using AOP, you can define a crosscuting concern somewhere, and then use it anywhere you want. It is reusable ! This can not be done with the partial keyword ! Moreover, using a tool like AspectDNG, you are not bound to the concept of “class”, you can inject you crosscuting concern very precisely.

    AOP and partial classes are strongly different.

Comments