0

I have a single CosmosDB container that I would like to hook a change feed into. This container has a few different data structures stored inside of it, and normally would query by a data type tag. I would like to handle the change feed differently based on which object type is updated. Is there a way to attach a query onto the change feed, or should I try and set the return data type as a Dictionary or dynamic?

1 Answers1

1

I am not aware of any partial processing or querying in change feed processor library.

But you can do it in this way by just passing a JObject to the ChangesHandler:

var changesHandler = new Container.ChangesHandler<JObject>(async (IReadOnlyCollection<JObject> collection, CancellationToken token) =>
{
   /// First deserialize just to get the type of entity/class
   var typedObject = JsonConvert.DeserializeObject<TypedObject>(doc.ToString());

   if (typedObject.DataType == "yourdatatypetohandle1")
   {
       /// Do sth. with this item
       var entity1 = JsonConvert.DeserializeObject<EntityToHandle1>();
   }
   if (typedObject.DataType == "yourdatatypetohandle2")
   {
       /// Do sth. with this item
       var entity2 = JsonConvert.DeserializeObject<EntityToHandle2>();
   }
}

With some kind of model entities/classes:

public class TypedObject 
{
    public virtual string DataType { get; set; }
}

public class EntityToHandle1 : TypedObject 
{
    public override string DataType { get; set; } = "yourdatatypetohandle1";
}

public class EntityToHandle1 : TypedObject 
{
    public override string DataType { get; set; } = "yourdatatypetohandle2";
}

Note that this solution is from a performance perspective not that perfect because 2x deserializing is not the best option.

Martin
  • 3,096
  • 1
  • 26
  • 46