Beispiel #1
0
        public void Test_DomainModelSerializer()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]");
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ba.EntityModel.Rules.Add(new FutureOrPastDateRuleModel((DateTimePropertyModel)ba.EntityModel.Properties["born"], TimeSpan.Zero, false));

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre");
            bb.AddUri("BookUri", "books");

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);
            dm.Relations.Add(ba.EntityModel.Relations.Single());
            ModelBuilder mbRel = new ModelBuilder(ba.EntityModel.Relations.Single());
            mbRel.AddIdentity("id");
            mbRel.AddIdentity("rid");
            mbRel.AddIdentity("lid");
            mbRel.Rules.AddRequired("lid");
            mbRel.Rules.AddRequired("rid");

            DomainModelSerializer ser = new DomainModelSerializer();
            XmlDocument xmlDoc = ser.Serialize(dm);
            var dm2 = ser.Deserialize(xmlDoc);

            Assert.AreEqual(dm.Entities.Count, dm2.Entities.Count);
            Assert.AreEqual(dm.Relations.Count, dm2.Relations.Count);
            var en1 = dm.Entities.GetEnumerator();
            var en2 = dm2.Entities.GetEnumerator();
            while (en1.MoveNext() && en2.MoveNext())
            {
                Assert.AreEqual(en1.Current.Name, en2.Current.Name);
                Assert.AreEqual(en1.Current.Properties.Count, en2.Current.Properties.Count);
                Assert.AreEqual(en1.Current.Relations.Count, en2.Current.Relations.Count);
                Assert.AreEqual(en1.Current.Rules.Count, en2.Current.Rules.Count);
                var p1 = en1.Current.Properties.GetEnumerator();
                var p2 = en2.Current.Properties.GetEnumerator();
                while (p1.MoveNext() && p2.MoveNext())
                {
                    Assert.AreEqual(p1.Current.Name, p2.Current.Name);
                    Assert.AreEqual(p1.Current.Type, p2.Current.Type);
                    Assert.AreEqual(p1.Current.DefaultValue, p2.Current.DefaultValue);
                    Assert.AreEqual(p1.Current.GetType(), p2.Current.GetType());
                    if (p1.Current.Type == PropertyType.Enum)
                        Assert.AreEqual(((EnumPropertyModel)p1.Current).EnumType, ((EnumPropertyModel)p2.Current).EnumType);
                    else if (p1.Current.Type == PropertyType.Computed)
                        Assert.AreEqual(((ComputedPropertyModel)p1.Current).Formula, ((ComputedPropertyModel)p2.Current).Formula);
                    else if (p1.Current.Type == PropertyType.Number)
                        Assert.AreEqual(((NumberPropertyModel)p1.Current).IsInteger, ((NumberPropertyModel)p2.Current).IsInteger);
                    else if (p1.Current.Type == PropertyType.Sequence)
                    {
                        Assert.AreEqual(((SequencePropertyModel)p1.Current).SequenceType, ((SequencePropertyModel)p2.Current).SequenceType);
                        Assert.AreEqual(((SequencePropertyModel)p1.Current).SequenceId, ((SequencePropertyModel)p2.Current).SequenceId);
                    }
                }

                var r1 = en1.Current.Rules.GetEnumerator();
                var r2 = en2.Current.Rules.GetEnumerator();
                while (r1.MoveNext() && r2.MoveNext())
                {
                    Assert.AreEqual(r1.Current.GetType(), r2.Current.GetType());
                    if (r1.Current is RequiredRuleModel)
                        Assert.AreEqual((r1.Current as RequiredRuleModel).Property.Name, (r2.Current as RequiredRuleModel).Property.Name);
                    else if (r1.Current is UniqueRuleModel)
                    {
                        Assert.AreEqual((r1.Current as UniqueRuleModel).Properties.Count(), (r2.Current as UniqueRuleModel).Properties.Count());
                        var pren1 = (r1.Current as UniqueRuleModel).Properties.GetEnumerator();
                        var pren2 = (r2.Current as UniqueRuleModel).Properties.GetEnumerator();
                        while (pren1.MoveNext() && pren2.MoveNext())
                        {
                            Assert.AreEqual(pren1.Current.Name, pren2.Current.Name);
                        }
                    }
                    else if (r1.Current is FutureOrPastDateRuleModel)
                    {
                        var er1 = r1.Current as FutureOrPastDateRuleModel;
                        var er2 = r2.Current as FutureOrPastDateRuleModel;
                        Assert.AreEqual(er1.Future, er2.Future);
                        Assert.AreEqual(er1.Offset, er2.Offset);
                        Assert.AreEqual(er1.Property.Name, er2.Property.Name);
                    }
                }
            }

            var rn1 = dm.Relations.GetEnumerator();
            var rn2 = dm2.Relations.GetEnumerator();
            while (rn1.MoveNext() && rn2.MoveNext())
            {
                Assert.AreEqual(rn1.Current.Name, rn2.Current.Name);
                Assert.AreEqual(rn1.Current.Properties.Count, rn2.Current.Properties.Count);
                Assert.AreEqual(rn1.Current.Relations.Count, rn2.Current.Relations.Count);
                Assert.AreEqual(rn1.Current.Rules.Count, rn2.Current.Rules.Count);
                var p1 = rn1.Current.Properties.GetEnumerator();
                var p2 = rn2.Current.Properties.GetEnumerator();
                while (p1.MoveNext() && p2.MoveNext())
                {
                    Assert.AreEqual(p1.Current.Name, p2.Current.Name);
                    Assert.AreEqual(p1.Current.Type, p2.Current.Type);
                    Assert.AreEqual(p1.Current.DefaultValue, p2.Current.DefaultValue);
                    Assert.AreEqual(p1.Current.GetType(), p2.Current.GetType());
                }
            }
        }
Beispiel #2
0
        private static void CreateDomain()
        {
            var dm = new DomainModel();
            ModelBuilder ba = new ModelBuilder("Author");
            ba.AddIdentity("Id");
            ba.AddString("FirstName", 128);
            ba.AddString("LastName", 128);
            ba.AddBoolean("IsAlive", true);
            ba.AddInteger("NumberOfAwards");
            ba.AddDateTime("Born");
            ba.AddDecimal("Rating");

            ModelBuilder bb = new ModelBuilder("Book");
            bb.AddIdentity("Id");
            bb.AddString("Title", 256);
            bb.AddString("ISBN", 20);
            bb.AddDecimal("Price");
            bb.AddEnum<Genre>("Genre", Genre.Mistery);

            bb.Rules.AddRequired("title");
            bb.Rules.AddUnique("ISBN");

            var rel = ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author");
            var br = new ModelBuilder(rel);
            br.AddDateTime("WrittenOn");

            dm.Entities.Add(ba.EntityModel);
            dm.Entities.Add(bb.EntityModel);

            ModelBuilder bo = new ModelBuilder("Order");
            bo.AddDecimal("Total");
            bo.AddBoolean("Paid");
            bo.AddDateTime("CreatedOn");
            bo.AddDateTime("ShipmentDate");
            bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered");
            bo.AddUri("Number", "BO");
            dm.Entities.Add(bo.EntityModel);

            dms.Save(dm);

            using (var conn = new TestDatabaseService().GetSqlConnection())
            {
                conn.Open();
                var dbManager = new DatabaseManager(conn);
                SequenceProvider.Initialize(dbManager);
            }
        }