0

Let's say I created a few models via Entity Framework, and one of them is called Paper_Results. This is how the class might look:

public partial class Paper_Results
{
   public string Color { get; set; }
   public int Height { get; set; }
   public int Width { get; set; }
}

I want to use this class like a domain model. Now let's say I create a class the derives from Paper_Results with an added interface

public class Construction_Paper : Paper_Results, IMeasurementType
{
    [Required]
    public (new?) string Color { get; set; }
    [Required]
    [Range(1, Int32.MaxValue, ErrorMessage = "Value should be greater than or equal to 1")]
    public (new?) int Height { get; set; }
    [Required]
    [Range(1, Int32.MaxValue, ErrorMessage = "Value should be greater than or equal to 1")]
    public (new?) int Width { get; set; }
    public virtual string MeasurementType
    {
       get { return "inches"; }
    }
}

Now when I create my ViewModel, I'll used the derived class instead:

public class Construction_Paper_ViewModel
{
   Construction_Paper cp;
   List<Construction_Paper> cpList;
   string title;

   public Construction_Paper_ViewModel()
   {
       title = "Construction Paper";
       cp = new Construction_Paper();
       cpList = new List<Construction_Paper>();
   }
}

I know I should be using uint instead of int for non-negative integers, but I just wanted to add more data annotations to the code. What I'm asking is what is the best OOP technique to derive from Paper_Result class, so that I don't have to modify it at all. The reason is because if I create a new solution and/or project, I don't want to do any modifications to it when I auto-regenerate it using Entity Framework. Should I use shadowing? Or the new keyword in the derived class? Or do you guys have any other better ideas?

The auto-generated EF models do not contain 'virtual' in their methods, thus the reason why I brought up shadowing and the new keyword.

Andy Narain
  • 55
  • 2
  • 8

1 Answers1

1

First of all, not every problem should be addressed via inheritance.

Second, the .NET framework already has a mechanism to add metadata (attributes) to an existing object. These are called Buddy Classes, and use the MetadataTypeAttribute class.

The idea is that you add an attribute to the class that allows you to specify a different class that is used to define the metadata for the original class. It's not pretty, but it gets the job done.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Thanks. Can you also add additional properties/methods as well? I assume that you can. – Andy Narain Sep 25 '14 at 14:41
  • 1
    @AndyNarain - You have several options, you could make the class Partial to add additional items to it... but understand that Inheritance defines an "is-a" relationship. It shouldn't simply be used to save keystrokes. – Erik Funkenbusch Sep 25 '14 at 15:59
  • That's what I ended up doing. I created a buddy class with one additional property. – Andy Narain Sep 26 '14 at 18:05