简体   繁体   English

在派生类中分配受保护的属性

[英]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. 我在derived类的base类中分配(设置)受保护的属性时遇到麻烦。 I have a solution but I like to know if this is the best design pattern to use or is there a better way? 我有一个解决方案,但我想知道这是最好的design pattern还是有更好的方法?

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() 但是,我知道在派生构造函数中可以设置基本属性: 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. 我们应该倾向于尽可能避免重复,并使用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. 例如,在您的情况下,我相信您不提供To信息就无法发送电子邮件 ,因此最好通过构造函数提供To 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. 另外,在派生类中分配受保护的属性本身可能会成为一个问题,并且可能导致违反Liskov替代,Open-close和其他SOLID原则。 But, of course, sometimes it could be useful, and there is no general rule of not doing this. 但是,当然,有时它可能有用,并且没有不这样做的一般规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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