c# - 这是简化继承类的有效方法吗?

标签 c# azure azure-storage azure-table-storage

我有许多 Azure 类,其中包含日期字段并通过跟踪进行修改。字段例如:

[DisplayName("Created By")]
public string CreatedBy { get; set; }
[DisplayName("Modified By")]
public string ModifiedBy { get; set; }

我想避免重复,所以我正在考虑创建一个类来包含这些内容,例如:

public class TableServiceEntityRowInfo : TableServiceEntity  
{
    [DisplayName("Created By")]
    public string CreatedBy { get; set; }
    [DisplayName("Modified By")]
    public string ModifiedBy { get; set; }
}

对于我的数据类,我不想让它们继承自 TableServiceEntity,而是希望按如下方式进行设置:

public class MyClass : TableServiceEntityRowInfo 
{

}

这是向字段添加附加信息的有效且明智的方法吗?我在这里问的原因是因为我计划在很多类(class)中这样做,并且我想确保我做正确的事情。

最佳答案

是的,这是有效且合理的。

如果有任何特定于 Azure 的问题,我无法回答。

至于关于“IS A”与“HAS A”的评论,我认为这里可以安全地忽略它。实际上,这几乎可以归结为命名风格。您已将基类命名为 TableServiceEntityRowInfo,因为它是一个TableServiceEntity具有行信息。如果您将其称为 AuditableTableServiceEntity,会怎样,因为它具有用于审核的字段。现在它是完全相同的,但您可以声明每个继承类“IS A” AuditableTableServiceEntity

您可能还想将基类标记为abstract,以明确它不应被实例化,而只能被继承。

关于c# - 这是简化继承类的有效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8199476/

相关文章:

C# - FtpWebRequest 的多种方法?

azure - 如何根据 Kusto 中的状态汇总时间窗口

c# - 使用 BeginExecute 写入 Azure 表存储

azure - 有没有办法在azure blob存储中按层过滤

azure - ftp 到 azure 存储 blob(触发处理)

c# - 表达式的正确评估

c# - C Server 和 C# Server 之间的通信仅发送 "p"字符

c# - 为什么 SendBatch 从事件中心 .NET Standard 客户端中删除?有替代品吗?

c# - 对如何在 C# 中使用 JSON 感到困惑

Azure AD B2C,unauthorized_client 错误