Alternative way to use Azure Table Storage?

I would like to use such an entity to store tables:

public class MyEntity { public String Text { get; private set; } public Int32 SomeValue { get; private set; } public MyEntity(String text, Int32 someValue) { Text = text; SomeValue = someValue; } } 

But this is not possible because ATS needs

  • Constructor without parameters
  • All objects are public and read / write.
  • Inherit from TableServiceEntity;

The first two, two things that I do not want to do. Why should I want someone to change some read-only data? or create objects of this kind in an inconsistent manner (what is .ctor for this?), or even worse, change PartitionKey or RowKey. Why are we still limited by these deserialization requirements?

I don't like designing software this way, how can I use a table storage library so that I can serialize and deserialize myself? I think as long as objects inherit from TableServiceEntity, this should not be a problem.

For now I need to save the object, but I do not know how to get it:

  Message m = new Message("message XXXXXXXXXXXXX"); CloudTableClient tableClient = account.CreateCloudTableClient(); tableClient.CreateTableIfNotExist("Messages"); TableServiceContext tcontext = new TableServiceContext(account.TableEndpoint.AbsoluteUri, account.Credentials); var list = tableClient.ListTables().ToArray(); tcontext.AddObject("Messages", m); tcontext.SaveChanges(); 

Is there a way to avoid these deserialization requirements or get a raw object?

Greetings.

+4
source share
5 answers

The limitations around this ADO.NET shell for table storage are indeed somewhat painful. You can also use the Fat Entity approach implemented in Lokad.Cloud . This will give you much more flexibility regarding serializing your objects.

+4
source

If you want to use the Vault Client Library, then yes, there are restrictions on what you can and cannot do with your objects that you want to save. Point 1 is correct. I would add paragraph 2 to say β€œAll the properties you want to keep should be public and read / write” (for integer properties, you can get rid of read-only properties and they will not try to save them), but you Not Actually, you need to inherit from TableServiceEntity .

TableServiceEntity is a very lightweight class that has PartitionKey, RowKey, Timestamp properties and is decorated with the DataServiceKey attribute (take a look at Reflector). You can do all these things for a class that you create yourself and do not inherit from TableServiceEntity (note that this property is important).

If this still does not give you enough control over how you create your classes, you can always ignore the warehouse client library and just use the REST API directly. This will give you the ability to parse and deserialize XML in any way. You will lose all the nice things that come with the library, such as the ability to create queries in LINQ.

+4
source

Just don't use inheritance.

If you want to use your own POCOs, create your own class as you wish and create a separate tableEntity shell / container class that contains pK and rK and transfers your class as a serialized byte array.

+2
source

You can use the composition to achieve what you want. Build your table objects the way you need to store, and create your POCOs as wrappers for those who provide the API you need to see the rest of your application code. You can even mix some interfaces for better code.

+1
source

How about creating POCO shells at runtime using System.Reflection.Emit http://blog.kloud.com.au/2012/09/30/a-better-dynamic-tableserviceentity/

+1
source

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


All Articles