I have a container which I listen to it it's changes. Depending on the type of document I receive from the change feed, I handle each document differently as I populate or update my materialized view container for my read-heavy app.
In the latest extension, v4.x, the documents from the change feed passed into the function in a IReadOnly<T>
no longer takes Document as T and needs a concrete type.
Passing 'dynamic' throws an exception of 'can not cast System.string
' to System.IReadOnly<System.object>
which obviously tells that the function receives a JSON collection and deserialize it to a collection of T.
For now, as a workaround, locally in my development environment, I have created a function for each type I have in my collection and handle it as needed.
The drawback to this approach is that it will be expensive in production because for each and every update, all the functions are going to be invoked. Eating up my functions calls quota. So instead of a single function call, ten functions are going to be invoked, with 9 of them doing absolutely nothing, just checking if they have to do anything and then returning.
My types, all share a single base class which has four properties (id, pk, ttl, and label (this is a discriminating property which I use to tell apart my types). But using this base type as T in IReadOnly<T>
is not an option as well since after deserialization, all the other properties are lost except for the four it defines.
Assuming the serializer that's used is Newtonsoft you could use the type JObject
for deserialization. Read the label from it and depending on the label deserialize into one of your different classes.
var json = "{\"Label\": \"Example\", \"IsSuccess\": true }";
var jObject = JsonConvert.DeserializeObject<JObject>(json);
var label = (string) jObject.Property("Label");
if (label == "Example")
{
var example = jObject.ToObject<Example>();
Console.WriteLine(example.IsSuccess);
}
public class Example
{
public string Label { get; set; }
public bool IsSuccess { get; set; }
}