0

I haven't done this for a while and I need to find out if this is the best OO way to go. I am having trouble assigning (Setting) the protected properties in a base class in the derived class. I have a solution but I like to know if this is the best design pattern to use or is there a better way?

My Base class

public abstract class EmailBase
{
   protected string Subject { get; set; }
   protected string To { get; set; }
   protected string From { get; set; }

   protected virtual void Send()
   {
       using (MailMessage mail = new MailMessage())
       { 
            // Ok send message here...
       }
   }

}

I have two different email templates that I need to send so I thought it would be a good idea to have two derived classes, however I will post the code for one derived class for the problem at hand.

public class DerivedOne: EmailBase
{
    private const string emailTemplate = "some static text for the body...";

    public DerivedOne()
    {
    }

    // This is how I want to set the base class properties, 
    // but it feels I am just duplicating properties... 
    public string To
    {
        set
        {
            base.To = value;
        }
    }

And in the controller...

  // A send email button was pressed by the user

  private bool SendEmail(Model)
  {
        DerivedOne eMail = new DerivedOne()
        {
            To = Model.To;
        };
  }

I tend to not send the properties through the derived constructor as I believe setting up properties tends to be cleaner. However, I know in the derived constructor you can set the base properties : base()

So this is why I have asked, am I wrong to create the same properties in the derived class so the controller can see it? (as the protected properties cannot be seen outside of inheritance of course)

rene
  • 41,474
  • 78
  • 114
  • 152
user3428422
  • 4,300
  • 12
  • 55
  • 119
  • 1
    Why your properties in the base class are not public? – galenus Nov 05 '14 at 12:41
  • Doesn't this ruin the theory of polymorphism? (That wasn't a throw back, its what I think) So I guess I am wrong? – user3428422 Nov 05 '14 at 12:45
  • It enforces polymorphism because you can access any derived type through a reference of the base type. – galenus Nov 05 '14 at 12:49
  • I'd say: either make the properties abstract in your base class, or make them virtual. [Your choice](http://stackoverflow.com/questions/12254438/not-sure-when-to-use-an-abstract-property-and-when-not). – BCdotWEB Nov 05 '14 at 12:56
  • Please check this SO post for your reference http://stackoverflow.com/questions/12270941/cannot-access-protected-member-of-base-class-in-derived-class – Shadi Nov 05 '14 at 13:07

1 Answers1

0

Yes, I think that you right with your doubts. We should tend to avoid duplication wherever possible and use the full power of OOP.

Plus, you could avoid a lot of problems by making your classes immutable and providing dependencies via constructor. If class needs dependency to be consistent, this dependency should be provided via constructor. Doing this could guarantee yourself(and other programmers) that you can't create instance of class without providing this dependency. For example, in your case I believe you can't send Email without providing To information, so it's better to provide To via constructor. The same reasoning could be applied for other dependencies.

Plus, assigning protected properties in derived classes in itself could be a problem and could lead to violations of Liskov-substitution, Open-close and other SOLID principles. But, of course, sometimes it could be useful, and there is no general rule of not doing this.

alekseevi15
  • 1,732
  • 2
  • 16
  • 20