public void TestUpdatePropertyCollection() { PropertySchemaSet schemaSet = new PropertySchemaSet(new PropertySchemaFactory()); DateTime date = DateTime.Today; PropertySetCollection collection = new PropertySetCollection(schemaSet); collection.Add("v_int", new IntSchema()); collection.Add("v_string", new StringSchema()); collection.Add("v_date", new DateTimeSchema()); PropertySetPersistenceService persistenceService = new PropertySetPersistenceService(); Guid id = Guid.NewGuid(); persistenceService.SaveCollection(id, collection); IPropertySetCollection retrieved = persistenceService.FindCollection(id); Assert.IsNotNull(retrieved); Assert.IsNull(retrieved.Get <int?>("v_int")); Assert.AreEqual(DateTime.MinValue, retrieved.Get <DateTime?>("v_date")); Assert.IsNull(retrieved.Get <string>("v_string")); retrieved.Set("v_int", (int?)100); retrieved.Set("v_string", "hello"); retrieved.Set("v_date", (DateTime?)DateTime.Today); persistenceService.SaveCollection(id, retrieved); IPropertySetCollection retrieved1 = persistenceService.FindCollection(id); Assert.IsNotNull(retrieved1); Assert.AreEqual(100, retrieved1.Get <int?>("v_int")); Assert.AreEqual(DateTime.Today, retrieved1.Get <DateTime?>("v_date")); Assert.AreEqual("hello", retrieved1.Get <string>("v_string")); }
/// <summary> /// Deserialize schemas and collection elements /// </summary> /// <param name="collection"></param> /// <returns></returns> public static IPropertySetCollection Deserialize(this PersistentPropertyCollection collection) { PropertySchemaSet schemaSet = new PropertySchemaSet(new PropertySchemaFactory()); PropertySetCollection restored = new PropertySetCollection(schemaSet); //restore schemas foreach (PersistentPropertyElement element in collection.Elements) { PersistentSchemaElement schemaElement = collection.Schemas.First(s => s.SchemaName == element.Name); var valueSchema = JsonSchemaDeserializer.Deserialize(schemaElement.SchemaType, schemaElement.SchemaBody); SerializationTypeHint hint = (SerializationTypeHint)schemaElement.SerializationHint; Func <PersistentPropertyElement, object> valueRetriever; if (!_serializationMap.TryGetValue(hint, out valueRetriever)) { throw new ArgumentException($"{element.Name} {hint}"); } object val = valueRetriever(element); switch (hint) { case SerializationTypeHint.JsonString: val = valueSchema.Serializer.Deserialize(val.ToString()); break; case SerializationTypeHint.BinaryObject: case SerializationTypeHint.Object: byte[] data = (byte[])val; val = valueSchema.Serializer.Deserialize(data); break; } restored.Add(element.Name, val, valueSchema); } return(restored); }