Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make existing entity implement TableEntity

I'm trying to create a generic CRUD service for Azure Table Storage.

In the past I have always used SQL with a repository/unit of work pattern with Entity Framework. I want to have the same thing with Azure Table Storage but all the examples I have seen require that my entities implement TableEntity from the Azure lib.

However to me this is in conflict with SOLID principals - in that my repository and my models should not need to know about Azure to work.

So what I want is a service that I pass an entity too, and for that service to alter the said class to make it implement the TableEntity and thus allow me to run the usual TableStorage CRUD operations, map it BACK to my entity class and return it.

like image 483
BrettH Avatar asked Jan 27 '26 23:01

BrettH


2 Answers

I have written a generic API which does the conversion from any complex object to EntityProperty dictionary by flattening the complex object using recursive reflection. You can then write the entity property dictionary to Azure Table Storage as DynamicTableEntity.

When you Read the DynamicTableEntity, the API does the conversion back to the complex object.

The power of this API is that it works on any complex object with nested properties that themselves may be complex objects with other nested properties.

Feel free to have a look and use:) https://www.nuget.org/packages/ObjectFlattenerRecomposer/

Usage:

//Flatten object of type Order) and convert it to EntityProperty Dictionary
 Dictionary<string, EntityProperty> flattenedProperties = ObjectFlattenerRecomposer.Flatten(order);

// Create a DynamicTableEntity and set its PK and RK
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);

dynamicTableEntity.Properties = flattenedProperties;

// Write the DynamicTableEntity to Azure Table Storage using client SDK


//Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK

DynamicTableEntity entity = [Read from Azure using the PK and RK];

//Convert the DynamicTableEntity back to original complex object.    
Order order = ObjectFlattenerRecomposer.ConvertBack<Order>(entity.Properties);

That's all:)

like image 153
Dogu Arslan Avatar answered Jan 30 '26 11:01

Dogu Arslan


I figured it out myself. I used a Dynamic Object that implemented the requirements for ITableEntity.

If anyone is interested I wrote a blog about how I did it here.

http://bretthargreaves.wordpress.com/2014/01/11/azure-table-storage-with-repository-pattern/

like image 32
BrettH Avatar answered Jan 30 '26 12:01

BrettH