All the examples on the documentation mention that Entities on Azure Storage map to classes derived from TableEntity. However, this would not fit well with some of the design goals. This post is all about how to abstract this requirement to derive each class from TableEntity and keep everything tidy.
TableEntity class implements ITableEntity and it has two methods to read/write data to the object. We will be leveraging the same to come up with a solution.
Let’s create a base class, which we can use to derive all Entity classes.
PartitionKey, RowKey, Timestamp and ETag are the default properties each entity on Azure Storage possesses. StorageTableEntityBase class defined above lets us to contain these properties, copied over from the entity.
Let us create an adapter class which implements ITableEntity:
I have marked the class as internal to limit its access to current assembly. This class copies over properties such as PartitionKey, implements ReadEntity and WriteEntity methods to copy over rest of the properties to a temporary object InnerObject.
We can now define our Entity Class as follows:
This is all the code we need to get it working. Now we can write a method to read from Storage Table as follows:
I have not included the boilerplate methods instantiate TableClient, which is a trivial part and is available on the documentation.