0

I read that multiple inheritance doesn't exist in c# but can be mocked by using interfaces

I am trying to do something similar and it doesn't seem to be working. Wondering what I might be doing wrong. I've defined my concrete class to inherit from:

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

and then my "super" class that inherits from AppSettings directly and implements ITableEntity. This class should ultimately act as if it inherited from both AppSettings and 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);
        }
    }

The issue is that when I actually use the class and list rows from a table the AppSettings field show up as null.

If I do the opposite and inherit directly from TableEntity and implement AppSettings it works just fine however this is not desirable for me. AppSettings is likely to change with time and I don't want to have to maintain the two separate models every time a change happens.

What am I doing wrong?

Crhistian Ramirez
  • 1,066
  • 12
  • 20
  • And why multiple inheritance? To me, it seems that you are trying to have a class with 2 purposes which is contrary to the **Single Responsibility Principle (SRP)**. – Phil1970 Sep 07 '19 at 13:04
  • _I don't want to have to maintain the two separate models_ But you still want two have two independent classses?? – TaW Sep 07 '19 at 14:39

1 Answers1

0

The implementation of TableEntity uses reflection to enumerate the serializable properties of the concrete type. You don't need to inherit from TableEntity at all in this case, just implement your own serialization. Consider calling TableEntity.ReadUserObject(this, properties, operationContext) in your ReadEntity method.