سؤال

My concern is mainly with ServiceStack's Redis client. I have a bunch of entities that I want to store in cache. Their members are mostly encapsulated (private), and some of them don't have any public accessor. Do ServiceStack's serializers allow me to store (and get, obviously) those entities in Redis? Or do I have to create "clean" POCO classes with public properties for serialization, which will force me tomaintain 2 representations of any entity: A real one, and a "fake" POCO representation of it for storing it in Redis?

I hope the answer isn't the latter :)

هل كانت مفيدة؟

المحلول

The answer is the latter, by design. ServiceStack promotes a message-based design. There are many advantages to keeping your request/response DTO classes separate and single-purpose. The ServiceStack.Text JSON serializer is designed to serialize public properties, not public or private fields or private properties.

نصائح أخرى

public static string SerializeObject(object obj, BindingFlags membersType) {               
      var typeConfigType = typeof(TypeConfig<>).MakeGenericType(obj.GetType());

      var prop = typeConfigType.GetMembers(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(item => item.Name == "Fields") as PropertyInfo;
      prop.SetValue(null, obj.GetType().GetFields(membersType), null);

      prop = typeConfigType.GetMembers(BindingFlags.Public | BindingFlags.Static).FirstOrDefault(item => item.Name == "Properties") as PropertyInfo;
      prop.SetValue(null, new PropertyInfo[0], null);

      return JsonSerializer.SerializeToString(obj);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top