GithubHelp home page GithubHelp logo

corehelpers / azurestoragetable Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 5.0 285 KB

This projects implements an abstraction for Azure Storage Tables to use POCOs

License: MIT License

C# 100.00%
poco dotnet-core dotnet azure azure-storage azure-table-storage

azurestoragetable's Introduction

Build Status

AzureStorageTable

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.

Installation

Install-Package CoreHelpers.WindowsAzure.Storage.Table

Manual Entity Mapper

// create a new user model
var user = new UserModel() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };

using (var storageContext = new StorageContext(storageKey, storageSecret))
{
  // configure the entity mapper
  storageContext.AddEntityMapper(typeof(UserModel), new DynamicTableEntityMapper() { TableName = "UserProfiles", PartitionKeyPropery = "Contact", RowKeyProperty = "Contact" });

  // ensure the table exists
  storageContext.CreateTable<UserModel>();

  // inser the model
  storageContext.MergeOrInsert<UserModel>(user);

  // query all
  var result = storageContext.Query<UserModel>();

  foreach (var r in result)
  {
    Console.WriteLine(r.FirstName);
  }
}

Attribute Based Entity Mapper

Decorate your existing model

[Storable()]
public class UserModel2
{                       
  [PartitionKey]
  [RowKey]
  public string Contact { get; set; }

  public string FirstName { get; set; } 
  public string LastName { get; set; }                		
}

Configure and use the Storage Context

// create a new user model
var user = new UserModel2() { FirstName = "Egon", LastName = "Mueller", Contact = "[email protected]" };            

using (var storageContext = new StorageContext(storageKey, storageSecret))
{
  // ensure we are using the attributes
  storageContext.AddAttributeMapper();

  // ensure the table exists
  storageContext.CreateTable<UserModel2>();

  // inser the model
  storageContext.MergeOrInsert<UserModel2>(user);

  // query all
  var result = storageContext.Query<UserModel2>();

  foreach (var r in result)
  {
      Console.WriteLine(r.FirstName);
  }
}

Virtual Partition and Row-Keys

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}}")]
public class VirtualPartKeyDemoModel
{
  public string Value1 { get; set;  }
  public string Value2 { get; set;  }				
  public string Value3 { get; set;  }
}

Virtual Array Attributes

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")]
public class VArrayModel
{
  [PartitionKey]
  [RowKey]
  public string UUID { get; set; }

  [VirtualList(PropertyFormat: "DE{{index}}", Digits: 2)]
  public List<int> DataElements { get; set; } = new List<int>();
}

Virtual Dictionary Attributes

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")]
public class VDictionaryModel
{
  [PartitionKey]
  [RowKey]
  public string UUID { get; set; }

  [VirtualDictionary(PropertyPrefix: "DE")]
  public Dictionary<string, int> DataElements { get; set; } = new Dictionary<string, int>();
}

Store as JSON Object Attribute

The store as JSON attribute allows to store refenrenced objects as json payload for a specific property

[Storable(Tablename: "JObjectModel")]
public class JObjectModel
{
 [PartitionKey]
 [RowKey]
 public string UUID { get; set; }
 
 [StoreAsJsonObject]
 public Dictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
}

Contributing to Azure Storage Table

Fork as usual and go crazy!

Contributors Thank you to the following wonderful people for contributing to Azure Storage Table:

azurestoragetable's People

Contributors

afrischk avatar davidvollmers avatar dei79 avatar petero-dk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

azurestoragetable's Issues

Storable Attribute Table name

  1. I tried to use this for my Entity Model.. but got an error that gave me an error about getItem not found in Dictionary.. for a fully qualified model name (namespace with model name) when I called

await storageContext.CreateTableAsync();

[Storable("MyTableName")]

  1. After a while of troubleshooting.. I saw your example that explicity specifies Tablename.. so I tried it and it worked.. just wanted to let you know.

[Storable(Tablename: "MyTableName")]

Thank you.

It could be something else on my end but I wanted to pass along just in case..

Omission of declaration in Interface IStorageContext

	Hello
	I started to use your sdk for my personal projects
	I have some questions.
    in class public class StorageContext	there are two pairs of methods
	
	public async Task InsertOrReplaceAsync<T>(IEnumerable<T> models) where T : new()
	public async Task InsertOrReplaceAsync<T>(T model) where T : new()

	public async Task MergeOrInsertAsync<T>(IEnumerable<T> models) where T : new()
	public async Task MergeOrInsertAsync<T>(T model) where T : new()
	
	In Interface IStorageContext
	You do not declare 
	public  Task InsertOrReplaceAsync<T>(IEnumerable<T> models) where T : new()

    Is this is intentional or simply omission?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.