Storing object identifiers using Redis, key or Id?

I have a main base class for (almost) all POCO classes in my application.

public abstract class AceOfBase
{
    public long Id { get; set; }
    public DateTimeOffset CreatedOn { get; set; }
    public string Key { get; set; }
}

Some of my main classes ( Member , Building, Community, etc.) have enough properties, so I plan to use the easy-to-manage ServiceStack Strongly-typed client .

From my research, it seems that the client uses a structured methodology to create keys

Essentially, POCOs are stored in Redis as serialized JSON with the name typeof (Poco) .Name and the identifier used to generate the unique key for this instance. For example:

urn: Poco: {Id} => '{"Id": 1, "Foo": "Bar"}'

Since from time to time I get an object that inherits the base class (but which I don’t know the derived class at compile time), how can I best store the key so that I can get any object easily?

An ideal world that I could customize RedisClientand RedisTypedClientuse the same naming convention for keys, but I did not find anything that allows me to do this (for now), and the documentation on the individual methods is difficult for both clients.

+4
source share
1 answer

After scrolling through the ServiceStack assembly, I found that the strongly typed client Store()method is actually just SetEntry(key,value)using the utility method to generate the key structure.

IdUtils.CreateUrn , Type Id, , .

CreateKey , , - AceOfBase Key.

//In BaseRepository
public virtual string CreateKey<T>(T entity) where T : AceOfBase
{
 return IdUtils.CreateUrn<AceOfBase>(entity.Id);
}

//In MemberRepository
Member m = new Member(); //inherits from AceOfBase
m.Key = CreateKey(m);

, Redis, , , EF.

  • DomainModels. , .

    public class Activity : AceOfBase
    {
        public AceOfBase IndirectObject { get; set; }
        public Participant Predicate { get; set; }
        public Participant Subject { get; set; }
        public Verb Verb { get; set; }
    }
    
  • DataModels. DomainModel, , Redis, ( ), .

    public class Activity : AceOfBase
    {
        public string IndirectObject { get; set; }
        public string Predicate { get; set; }
        public string Subject { get; set; }
        public Verb Verb { get; set; }
    }
    

AutoMapper TypeConverter, DataModel DomainModel. AutoMapper , Redis .

//In Configure()
Mapper.CreateMap<string,Member>().ConvertUsing<KeyToBaseConverter<Member>>();
Mapper.CreateMap<Member, string>().ConvertUsing<BaseToKeyConverter<Member>>();

public class KeyToBaseConverter<T> : ITypeConverter<string, T> where T : AceOfBase
{
    public RedisRepository Repository { get; set; }
    public T Convert(ResolutionContext context)
    {
        return Repository.GetByKey<T>(context.SourceValue.ToString());
    }
}
public class BaseToKeyConverter<T> : ITypeConverter<T, string> where T : AceOfBase
{
    public string Convert(ResolutionContext context)
    {
        var f = context.SourceValue as AceOfBase;
        return f.Key;
    }
}

, , , Lists Sets, , .

, , , JBON blob Key.

+1

All Articles