Deploy an interface and use code from an existing interface implementation

I am trying to implement the ITableEntity interface to add the [DataContract] attribute to it. But if I implement this interface myself, I will have to give the ReadEntity and WriteEntity body.

But there is a class that already implements the ITableEntity interface and gave the ReadEntity and WriteEntity body, which is TableEntity.cs .

How can I make my interface implementation use methods in the TableEntity class?

[change]

 [DataContract] public class SerializableTableEntity : ITableEntity { private TableEntity tableEntity; public string ETag { get; set; } public string PartitionKey { get; set; } public string RowKey { get; set; } public DateTimeOffset Timestamp { get; set; } public SerializableTableEntity() { tableEntity = new TableEntity(); } public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext) { tableEntity.ReadEntity(properties, operationContext); } public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext) { return tableEntity.WriteEntity(operationContext); } } 
+4
source share
4 answers

In the end, I made an exact copy of these classes and made them Serializable . But the ability to execute complex queries seems to be complex too. So, we have moved to SQL Database.

+1
source

The reason that every property in your stored table is empty is because WriteEntity and ReadEntity use an empty object to store and write data.

You delegate the serialization of your object to 'tableEntity', but none of your properties exist.

Suggestion: you will need to implement all your SerializableTableEntity properties inside a class that comes from TableEntity, contain a variable of this type inside the SerializableTableEntity object and delegate each get / set member property from SerializableTableEntity to this new object.

It makes sense?

EDIT: sample code as requested (you wont get it though)

  [DataContract] public class SerializableTableEntity : ITableEntity { private CustomEntity tableEntity; public string ETag { { get { return tableEntity.ETag; } set { tableEntity.Etag = value; } } public string PartitionKey { get { return tableEntity.PartitionKey; } set { tableEntity.PartitionKey = value; } } public string RowKey { get { return tableEntity.RowKey; } set { tableEntity.RowKey = value; } } public DateTimeOffset Timestamp { get { return tableEntity.Timestamp; } set { tableEntity.Timestamp = value; } } public string PropertyOne { get { return tableEntity.PropertyOne; } set { tableEntity.PropertyOne = value; } } public SerializableTableEntity() { tableEntity = new CustomEntity(); } public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext) { tableEntity.ReadEntity(properties, operationContext); } public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext) { return tableEntity.WriteEntity(operationContext); } } public class CustomEntity : TableEntity { public string PropertyOne { get; set; } } 
+3
source

Or delegate "uninteresting" methods (a more realistic example here ):

 class YourClass : Interface { public void ReadEntity() { delegateTo.ReadEntity(); } TableEntity delegateTo = new TableEntity(); } 

or just throw an exception from them (for example, NotImplementedException ) - the latter will work only for you if these methods are not called.

0
source

You can create a class that contains the implementation of the TableEntity class, but also adds the necessary functions. This is similar to Decorator Pattern .

 [Attributes...] public class MyTableEntity : ITableEntity { private TableEntity decoratedTableEntity; public void ReadEntity(args...) { decoratedTableEntity.ReadEntity(args...); } } 

To make the solution more universal, change the stolen value of TableEntity to ITableEntity.

0
source

Source: https://habr.com/ru/post/1499373/


All Articles