The buddy classes are drowning DRY

time to read 2 min | 380 words

image The notion of buddy classes has appeared at first in ASP.Net Dynamic Data, and now has made its way to RIA services. I don’t really bother to keep track with all the new tech from Microsoft, but this post came to my attention.

What is a buddy class? Let us take this example as an example, which was generate by a tool:

public partial class MyClass
{
    public int MyProperty { get; set; }
}

And say that we need to add some attribute to that, to add instruction for tools support. We can’t do that, because MyClass is generated by a tool. ASP.Net Dynamic Data came up with the following scheme:

[MetadataClass(typeof(MyMetadataClass))]
public partial class MyClass
{
    public int MyProperty { get; set; }
}

public class MyMetadataClass
{
    [Range(1,100)]
    public int MyProperty{ get; set; } 
}

This make me feel ill.

DRY is one of the few truly universal CS principles, and here we have a idiom that not only ignores it, but encourage repeating ourselves over and over and over again.

No doubt this is an easy solution, easy to document, easy to implement, easy to work with. It is also a maintenance nightmare, and cause sever harm to the project. If you want to create external metadata to generated classes, there are other ways of doing that, violating DRY as a systematic approach is a mistake that will come back to haunt you.