使用TableEntity

时间:2019-09-07 12:52:55

标签: c# azure azure-table-storage

我读到c#中不存在多重继承,但是can be mocked by using interfaces

我正在尝试做类似的事情,但似乎没有用。想知道我可能做错了什么。我已经定义了具体的类来继承:

public class AppSettings {
    public string CosmosDbPrimaryKey {get; set;}
}

,然后是我的“超级”类,该类直接从AppSettings继承并实现ITableEntity。此类最终应该像从AppSettings和TableEntity继承一样。

public class AppSettingsRow: AppSettings, ITableEntity
    {
        private readonly TableEntity _tableEntity;
        public AppSettingsRow()
        {
            _tableEntity = new TableEntity();
        }

        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public DateTimeOffset Timestamp { get; set; }
        public string ETag { get; set; }

        public void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
        {
            _tableEntity.ReadEntity(properties, operationContext);
        }

        public IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
        {
            return _tableEntity.WriteEntity(operationContext);
        }
    }

问题在于,当我实际使用类并列出表中的行时,AppSettings字段显示为空。

如果我做相反并直接从TableEntity继承并实现AppSettings,则可以正常工作,但是这对我来说不是理想的。 AppSettings可能会随着时间而改变,我不想每次发生更改时都必须维护两个单独的模型。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

TableEntity的实现使用反射来枚举具体类型的可序列化属性。在这种情况下,您根本不需要继承TableEntity,只需实现自己的序列化即可。考虑在您的TableEntity.ReadUserObject(this, properties, operationContext)方法中调用ReadEntity