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