コード例 #1
0
        /// <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);
        }
コード例 #2
0
        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"));
        }
コード例 #3
0
        public void TestSavePropertyCollection()
        {
            PropertySchemaSet     schemaSet  = new PropertySchemaSet(new PropertySchemaFactory());
            PropertySetCollection collection = new PropertySetCollection(schemaSet)
            {
                { "V_string", "hello", schemaSet.SchemaFactory.Create(typeof(string)) },
                { "V_int", 100, schemaSet.SchemaFactory.Create(typeof(int?)) },
                { "V_bool", true, schemaSet.SchemaFactory.Create(typeof(bool?)) },
                { "V_date", DateTime.Now, schemaSet.SchemaFactory.Create(typeof(DateTime)) }
            };
            PropertySetPersistenceService persistenceService = new PropertySetPersistenceService();
            Guid id = Guid.NewGuid();

            persistenceService.SaveCollection(id, collection);
        }
コード例 #4
0
        public void TestLoadSavedPropertyCollection()
        {
            PropertySchemaSet     schemaSet  = new PropertySchemaSet(new PropertySchemaFactory());
            DateTime              date       = DateTime.Today;
            PropertySetCollection collection = new PropertySetCollection(schemaSet)
            {
                { "V_string", "hello", schemaSet.SchemaFactory.Create(typeof(string)) },
                { "V_int", 100, schemaSet.SchemaFactory.Create(typeof(int?)) },
                { "V_bool", true, schemaSet.SchemaFactory.Create(typeof(bool?)) },
                { "V_date", date, schemaSet.SchemaFactory.Create(typeof(DateTime)) }
            };
            PropertySetPersistenceService persistenceService = new PropertySetPersistenceService();
            Guid id = Guid.NewGuid();

            persistenceService.SaveCollection(id, collection);
            IPropertySetCollection retrieved = persistenceService.FindCollection(id);

            Assert.IsNotNull(retrieved);
            Assert.AreEqual("hello", retrieved.Get <string>("V_string"));
            Assert.AreEqual(100, retrieved.Get <int>("V_int"));
            Assert.AreEqual(date, retrieved.Get <DateTime>("V_date"));
            Assert.IsTrue(retrieved.Get <bool>("V_bool"));
        }