1

I've seen the term "Buddy class" used as an 'answer' to questions like "how can I add annotations to a partial class in another file" but these answers assume I know what a Buddy Class is, and the code examples assume I understand how/why this works.

I couldn't see a simple explanation of what a buddy class in C# is, and how/why it allows me to modify an existing class such as adding annotations to properties.

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Which part of "assumes I know what a buddy class _is_" was unclear? – Mr. Boy Jul 14 '16 at 10:20
  • That question makes no explanation of what is going on, it just asks why people do it that way. – Mr. Boy Jul 14 '16 at 10:22
  • You cannot define the same property twice in two partial classes... http://stackoverflow.com/questions/6131754/how-to-add-data-annotations-to-partial-class – Mr. Boy Jul 14 '16 at 10:25
  • @MatíasFidemraizerI agree with OP that the question he asked is different from the answer that was proveded. I think something like [this](https://hartzer.wordpress.com/2010/01/26/mvc-buddy-class/) is what he was looking for. – 3615 Jul 14 '16 at 10:27
  • I've re-opened it, let's see if OP gets further info on the topic that's not already in similar Q&As.. – Matías Fidemraizer Jul 14 '16 at 10:28

1 Answers1

5

‘Buddy class’ is not necessarily C# specific but I believe it is more commonly seen in .Net as its sort of a pattern, or technique (hack), used to extend auto generated classes and add attributes to them. They are also referred to as associated classes sometimes, or meta data classes. The naming convention is to append MD (for meta data) to the buddy class so it can be identified as one. As for why, well- auto generated code will overwrite any changes you make. Associated classes could be a way to circumvent that, and you could keep your custom meta data (for example validation attributes). You have one class that is auto generated, handily marked as partial (I believe that is actually why the partial modifier was introduced- to extend auto generated classes). You want to apply an attribute so you create a separate class that contains that, and you buddy it up with the other class.

If VS generates this for one of your entitites:

public partial class AutoGeneratedClass
{
    public string SomeData { get; set; }
}

And you want to extend that and add custom meta data you could create this:

[MetadataType(typeof(NotAutoGeneratedClassMD))]
public partial class AutoGeneratedClass
{
}

public class NotAutoGeneratedClassMD
{
    [DisplayName("This is some data")]
    public string SomeData { get; set; }
}

Short version:

What: Way to associate classes to extend an auto generated class with custom meta data

Why: Avoid having your changes to an auto generated class be overwritten when generated again.

Personally I'm not a fan, but that is a different story :)

Iris Classon
  • 5,752
  • 3
  • 33
  • 52
  • Thanks, this clarifies somewhat although the existence of `[MetadataType]` is a bit weird. Also that you _can_ define the same property twice this way, but not using two partial classes of the same name (which would seem to be the 'right' way) – Mr. Boy Jul 14 '16 at 13:41
  • No problem. Make sure to mark the answer so others can find the question and answer as well :) – Iris Classon Jul 30 '16 at 17:54