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?

2
  • 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). Commented Sep 7, 2019 at 13:04
  • I don't want to have to maintain the two separate models But you still want two have two independent classses?? Commented Sep 7, 2019 at 14:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.