문제

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