Я пытаюсь добавить сложный объект в Redis, но при получении значений из Redis я получаю некоторые значения как нулевые. Вот примерный пример, который я пробую. У меня есть сложный объект, я сериализую этот сложный объект с помощью JsonConvert и добавляю его в Redis. Свойство CollectionID имеет два значения с соответствующими значениями, но после получения его от Redis и десериализации делает значение нулевым.
Проблема заключается в том, что класс «Customer» является объектом-сущностью, а json serialize не может сериализовать этот конкретный объект c#. Есть ли возможное решение для этого сценария?
class Program
{
private static IDatabase _cache;
private static ConnectionMultiplexer _connection;
static void Main(string[] args)
{
_connection = ConnectionMultiplexer.Connect("localhost");
_cache = _connection.GetDatabase();
List<Id> id = new List<Id>() { new Id() { ID = 10 }, new Id() { ID = 20 } };
Collection<Customers> collection = new Collection<Customers>() { new Customers(id) };
Product product = new Product(new Guid(), collection, 1);
_cache.StringSet("Redis_Key", GetSerializedString(product));
var value = JsonConvert.DeserializeObject<Product>(_cache.StringGet("Redis_Key"));
}
private static String GetSerializedString<Test1>(Test1 value)
{
return JsonConvert.SerializeObject(
value,
Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.All
});
}
}
public class Product
{
public Product(
Guid parentGuid,
Collection<Customers> collection,
int number)
{
_parentGuid = parentGuid;
_collection = collection;
_number = number;
}
private Guid _parentGuid;
public Guid ParentGuid
{
get { return _parentGuid; }
}
private Collection<Customers> _collection;
public Collection<Customers> Collection
{
get { return _collection; }
}
private int _number;
public int number
{
get { return _number; }
}
}
public class Customers : Entity
{
.
.
.
public Customers(IEnumerable<Id> id)
{
_id = id;
}
private IEnumerable<Id> _id;
public IEnumerable<Id> CollectionID
{
get { return _id; }
}
.
.
.
}
public class Id
{
public int ID { get; set; }
}