1

I'm trying to figure out if there is any way to create a RedisClient that has the functionality of a RedisTypedClient but able to define the URN key with a simple string instead of passing in a type. For example, at the moment we're creating the IRedisTypedClient like so:

var redisProjectClient = _redisClientsManager.GetClient().As<Project>()

We can then store/retrieve related entities based on types that we know about:

var files = redisProjectClient.GetRelatedEntities<File>(projectId);

However, we're wanting to manage simple JSON payloads of objects from external services (fetched by HTTP API endpoints). These payload objects will be types that we won't know about in c# but will provide schema's/type names that we want to be able to use to manage the relationships like we do in the redis typed client. I can't see if this is currently possible without having to manually manage all of the extra stuff that makes the typed clients so good:

  • Id listing for entities of that type
  • The ability to retrieve/update entities related to an object
  • GetNextSequence for next Id

These aren't available in a flat IRedisClient so I want to do something like this:

var file = "{...}" // JSON object provided by external service

// We will know it's a "Project" type with a "projectID" from the JSON payload data:

var redisProjectClient = _redisClientsManager.GetClient().As("Project"); 
redisProjectClient.StoreRelatedEntities("File", projectId, file);


// ...

var files = redisProjectClient.GetRelatedEntities("File", projectId);

Any idea if this is possible or how to create this type of client?

Tom 'Blue' Piddock
  • 2,131
  • 1
  • 21
  • 36

1 Answers1

1

RedisTypedClient - Can you use strings to define the type?

No the Typed clients only works with Types, not unknown data structures, you'll need to store any arbitrary JSON payloads using the string IRedisClient.

To maintain the relationships you can store them in Redis sets as described in this previous answer which is what the StoreRelatedEntities() API does behind the scenes where it stores the entities using StoreAll() as normal but also maintains the related entities in an index Set using the urn (e.g. urn:File:x) as the identifier that each entity is stored against.

GetRelatedEntities() then reads all ids maintained in the "relationship set index" and uses GetValues() to fetch all entities by urns.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Ah, I see - those extra helper functions in the typed client are really useful so I was hoping to be able to define them with a string that represents the unique classes we get in the payloads. Thank you for the quick reply and links - I'll try your advice! – Tom 'Blue' Piddock Jul 12 '19 at 08:49