public void TestEntityCreation()
        {
            var entity = new KonekoDynamicEntity {
                Name = "financial_accounts",
                Fields = new[] {
                    new SimpleDynamicField {
                            Name = "name",
                            DataType = typeof(string),
                            IsRequired = true,
                            DefaultValue = "n/a",
             							Properties = new Dictionary<string, object> {
                                { EntityDataTypeProperty.MaxLength, 100 }
                            }
                    },

                    new SimpleDynamicField {
                            Name = "amount",
                            DataType = typeof(decimal),
                            IsRequired = true,
                            DefaultValue = 1000,
             							Properties = new Dictionary<string, object> {
                                { EntityDataTypeProperty.DecimalSignsBeforePoint, 10 },
                                { EntityDataTypeProperty.DecimalSignsAfterPoint, 2 }
                            }
                    }
                }
            };

            Console.WriteLine(
                String.Format(
                    "Default value of dynamicentity:{0}:{1} equals {2}",
                    entity.Name,
                    "amount",
                    entity["amount"].GetTypedDefaultValue<decimal>()
            ));

            Console.WriteLine(
                String.Format(
                    "Property {0} of dynamicentity:{1}:{2} equals {3}",
                    EntityDataTypeProperty.DecimalSignsAfterPoint,
                    entity.Name,
                    "amount",
                    entity["amount"].GetTypedPropertyValue<short>(EntityDataTypeProperty.DecimalSignsAfterPoint)
            ));
        }
Example #2
0
        public void StoreAndRetrieve()
        {
            //StartMongoDb();
            var storage = new DynamicEntityMetadataStorage {
                ConnectionString = "mongodb://localhost/test_dynamic_entity"
            };

            var loadedEntity0 = storage.Load("financial_accounts");
            Assert.IsNull(loadedEntity0);

            var entity = new KonekoDynamicEntity {
                Name = "financial_accounts",
                Fields = new[] {
                    new SimpleDynamicField {
                            Name = "name",
                            DataType = typeof(string),
                            IsRequired = true,
                            DefaultValue = "n/a",
             							Properties = new Dictionary<string, object> {
                                { EntityDataTypeProperty.MaxLength, 100 }
                            }
                    },

                    new SimpleDynamicField {
                            Name = "amount",
                            DataType = typeof(decimal),
                            IsRequired = true,
                            DefaultValue = 1000,
             							Properties = new Dictionary<string, object> {
                                { EntityDataTypeProperty.DecimalSignsBeforePoint, 10 },
                                { EntityDataTypeProperty.DecimalSignsAfterPoint, 2 }
                            }
                    }
                }
            };

            Assert.AreEqual(1000, entity["amount"].GetTypedDefaultValue<decimal>());

            var res = storage.Create(entity);

            Assert.AreEqual(1, res.AffectedRecords);

            var loadedEntity = storage.Load("financial_accounts");

            Assert.AreEqual(100, loadedEntity["name"].GetTypedPropertyValue<int>(EntityDataTypeProperty.MaxLength));
            Assert.AreEqual(1000, loadedEntity["amount"].GetTypedDefaultValue<decimal>());

            loadedEntity["amount"].DefaultValue = 500;

            Assert.AreEqual(500, loadedEntity["amount"].GetTypedDefaultValue<decimal>());

            res = storage.Update(loadedEntity);
            Assert.AreEqual(1, res.AffectedRecords);

            var loadedEntity2 = storage.Load("financial_accounts");
            Assert.AreEqual(500, loadedEntity2["amount"].GetTypedDefaultValue<decimal>());

            res = storage.Delete("financial_accounts");

            Assert.AreEqual(1, res.AffectedRecords);

            var loadedEntity3 = storage.Load("financial_accounts");
            Assert.IsNull(loadedEntity3);

            //StopMongoDb();
        }
            public static IDynamicEntity FromBsonDocument(BsonDocument doc)
            {
                var fields = new List<IDynamicField>();
                foreach (var f in doc["fields"].AsBsonArray) {
                    var fDoc = f.AsBsonDocument;
                    // TODO: here should be factory method that will return correct field class, as for now always create SimpleDynamicField instance
                    var field = new SimpleDynamicField {
                        Name = fDoc["name"].AsString,
                        DataType = Type.GetType(fDoc["datatype"].AsString)
                    };
                    if (fDoc.Contains("default_value")) {
                        field.DefaultValue = fDoc["default_value"].RawValue; // ?
                    }
                    if (fDoc.Contains("properties")) {
                        field.Properties = fDoc["properties"].AsBsonArray.ToDictionary(k => k.AsBsonDocument["name"].AsString, v => v.AsBsonDocument["value"].RawValue);
                    }
                    fields.Add(field);
                }

                // TODO: here should be factory method that will return correct entity class, as for now always create DynamicEntity instance
                var result = new DynamicEntityImpl {
                    Name = doc["name"].AsString,
                    Fields = fields.ToArray()
                };
                return result;
            }