Exemple #1
0
        public void PersistAndFind()
        {
            var bindings = Emf.Configuration.Binding;

            Assert.IsTrue(bindings.RegisterKeyBinding(typeof(EntityA), new KeyBindingEntityA()));
            Assert.IsFalse(bindings.RegisterKeyBinding(typeof(EntityA), new KeyBindingEntityA()));
            Assert.IsTrue(bindings.RegisterKeyBinding(typeof(EntityB), new KeyBindingEntityB()));
            {
                var cb = new ColumnBinding("e");
                Assert.IsTrue(bindings.RegisterColumnBinding(typeof(EntityC), cb));
                Assert.IsTrue(bindings.RegisterKeyBinding(typeof(EntityC), new KeyBinding <EntityC>(e => e.Name)));
            }

            var eb1 = new EntityB();

            TestBase.TestSerialization(eb1);

            var eb2 = new EntityB {
                A = new EntityA()
            };

            TestBase.TestSerialization(eb2);

            using (var em = Emf.CreateEntityManager())
            {
                em.Persist(eb1);
                Assert.IsNotNull(eb1.Key);
                Assert.IsFalse(string.IsNullOrEmpty(eb1.Key.Row));
                Assert.IsFalse(string.IsNullOrEmpty(eb1.Key.ColumnFamily));
                Assert.IsTrue(string.IsNullOrEmpty(eb1.Key.ColumnQualifier));
                Assert.IsTrue(eb1.Key.Row.StartsWith(typeof(EntityB).Name));

                em.Persist(eb2);
                Assert.IsNotNull(eb2.Key);
                Assert.IsFalse(string.IsNullOrEmpty(eb2.Key.Row));
                Assert.IsFalse(string.IsNullOrEmpty(eb2.Key.ColumnFamily));
                Assert.IsTrue(string.IsNullOrEmpty(eb2.Key.ColumnQualifier));
                Assert.IsTrue(eb2.Key.Row.StartsWith(typeof(EntityB).Name));
                Assert.IsTrue(eb2.A.Name.StartsWith(typeof(EntityA).Name));
            }

            using (var em = Emf.CreateEntityManager())
            {
                var _eb1 = em.Find <EntityB>(eb1.Key);
                Assert.AreEqual(eb1, _eb1);
                Assert.IsTrue(_eb1.Key.Row.StartsWith(_eb1.GetType().Name));

                var _ea2 = em.Find <EntityA>(eb2.A.Name); // EntityA key binding uses the Name property as row key
                Assert.AreEqual(eb2.A, _ea2);

                var _eb2 = em.Find <EntityB>(eb2.Key);
                Assert.AreEqual(eb2, _eb2);
                Assert.IsTrue(_eb2.Key.Row.StartsWith(typeof(EntityB).Name));
                Assert.AreEqual(eb2.A.Name, _eb2.A.Name);
                Assert.IsTrue(eb2.A.Name.StartsWith(typeof(EntityA).Name));
            }

            var ec1 = new EntityC {
                Name = "11", A = new EntityA()
            };

            TestBase.TestSerialization(ec1);

            var ec2 = new EntityC {
                Name = "222", A = new EntityA(), B = new EntityB()
            };

            TestBase.TestSerialization(ec2);

            using (var em = Emf.CreateEntityManager())
            {
                em.Persist(ec1);
                Assert.AreEqual(ec1.Id, ec1.Name);

                em.Persist(ec2);
                Assert.AreEqual(ec2.Id, ec2.Name);
            }

            using (var em = Emf.CreateEntityManager())
            {
                var _ec1 = em.Find <EntityC>("11");
                Assert.AreEqual(ec1, _ec1);

                var _ec2 = em.Find <EntityC>(ec2);
                Assert.AreEqual(ec2, _ec2);
            }

            bindings.UnregisterBinding(typeof(EntityA));
            bindings.UnregisterBinding(typeof(EntityB));
            bindings.UnregisterBinding(typeof(EntityC));
        }