简体   繁体   中英

Assigning protected properties in derived class

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM