This projects implements an abstraction for Azure Storage Tables to use POCOs because deriving every entity from ITableEntity or TableEntity looks like a step backwards. The current implementation is intended to be an abstraction to store every existing entity into Azure Table Store.
There are two different principals implemented. The first allows to define an external mapping structure between the existing model and the required fields in Azure Table, e.g. Partition and RowKey. The second option is to decorate existing models with attributes to map the properties to partition and rowkey.
Install-Package CoreHelpers.WindowsAzure.Storage.Table // create a new user modelvaruser=newUserModel(){FirstName="Egon",LastName="Mueller",Contact="[email protected]"};using(varstorageContext=newStorageContext(storageKey,storageSecret)){// configure the entity mapperstorageContext.AddEntityMapper(typeof(UserModel),newDynamicTableEntityMapper(){TableName="UserProfiles",PartitionKeyPropery="Contact",RowKeyProperty="Contact"});// ensure the table existsstorageContext.CreateTable<UserModel>();// inser the modelstorageContext.MergeOrInsert<UserModel>(user);// query allvarresult=storageContext.Query<UserModel>();foreach(varrinresult){Console.WriteLine(r.FirstName);}}Decorate your existing model
[Storable()]publicclassUserModel2{[PartitionKey][RowKey]publicstringContact{get;set;}publicstringFirstName{get;set;}publicstringLastName{get;set;}}Configure and use the Storage Context
// create a new user modelvaruser=newUserModel2(){FirstName="Egon",LastName="Mueller",Contact="[email protected]"};using(varstorageContext=newStorageContext(storageKey,storageSecret)){// ensure we are using the attributesstorageContext.AddAttributeMapper();// ensure the table existsstorageContext.CreateTable<UserModel2>();// inser the modelstorageContext.MergeOrInsert<UserModel2>(user);// query allvarresult=storageContext.Query<UserModel2>();foreach(varrinresult){Console.WriteLine(r.FirstName);}}When implementing storage schemes in Azure Table sometimes the partition or the row key are combinations out for two or more properties. Because of that the Azure Storage Table components supports virtual partition and row key attributes as follows:
[Storable()][VirtualPartitionKey("{{Value1}}-{{Value2}}")][VirtualRowKey("{{Value2}}-{{Value3}}")]publicclassVirtualPartKeyDemoModel{publicstringValue1{get;set;}publicstringValue2{get;set;}publicstringValue3{get;set;}}When storing arrays in Azure Table store there are two options. The first option is to store it as a JSON payload and the second option is to expand the array with his items to separate properties, e.g.
{DataElements: [1,2,3,4] }becomes
| DE00 | DE01 | DE02 | DE03 |
|---|---|---|---|
| 1 | 2 | 3 | 4 |
in Azure Table Store with the following code:
[Storable(Tablename:"VArrayModels")]publicclassVArrayModel{[PartitionKey][RowKey]publicstringUUID{get;set;}[VirtualList(PropertyFormat:"DE{{index}}",Digits:2)]publicList<int>DataElements{get;set;}=newList<int>();}When storing dictionaries in Azure Table store there are two options. The first option is to store it as a JSON payload and the second option is to expand the dictionary with his items to separate properties in Azure Table Store with the following code:
[Storable(Tablename:"VDictionaryModels")]publicclassVDictionaryModel{[PartitionKey][RowKey]publicstringUUID{get;set;}[VirtualDictionary(PropertyPrefix:"DE")]publicDictionary<string,int>DataElements{get;set;}=newDictionary<string,int>();}The store as JSON attribute allows to store refenrenced objects as json payload for a specific property
[Storable(Tablename:"JObjectModel")]publicclassJObjectModel{[PartitionKey][RowKey]publicstringUUID{get;set;}[StoreAsJsonObject]publicDictionary<string,string>Data{get;set;}=newDictionary<string,string>();}Fork as usual and go crazy!
Contributors Thank you to the following wonderful people for contributing to Azure Storage Table: