Esempio n. 1
0
        public void ObjectContainerIsSerializable()
        {
            var binaryFormatter = new BinaryFormatter();

            pm.MakePersistent(m);
            pm.Save();
            var oc = pm.GetObjectContainer();

            Assert.That(Object.ReferenceEquals(m, oc.RootObjects[0]));

            oc.Formatter = binaryFormatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            var oc2 = pm.GetObjectContainer();

            Assert.AreEqual(0, oc2.RootObjects.Count);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, binaryFormatter);
            pm.MergeObjectContainer(oc);

            oc2 = pm.GetObjectContainer();
            Assert.AreEqual(1, oc2.RootObjects.Count);

            m = (Mitarbeiter)oc2.RootObjects[0];
            Assert.AreEqual("Mirko", m.Vorname);
            Assert.AreEqual(NDOObjectState.Persistent, m.NDOObjectState);
        }
Esempio n. 2
0
        public void CompleteTurnaroundWithAddedObject(Type formatterType)
        {
            // Create object and serialize it
            IFormatter formatter = (IFormatter)Activator.CreateInstance(formatterType);

            var m = CreateMitarbeiter();

            pm.MakePersistent(m);
            pm.Save();
            var oc = pm.GetObjectContainer();

            oc.Formatter = formatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            // Merge object into an OfflinePersistenceManager and change it
            OfflinePersistenceManager opm = new OfflinePersistenceManager(pm.NDOMapping.FileName);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, formatter);

            opm.MergeObjectContainer(oc);

            var m2 = new Mitarbeiter()
            {
                Vorname = "Hans", Nachname = "Müller"
            };

            opm.MakePersistent(m2);

            // Create a ChangeSetContainer and serialize it
            var csc = opm.GetChangeSet();

            csc.Formatter = formatter;
            string serializedChanges = csc.MarshalingString;

            // Merge the changes back to pm
            csc = new ChangeSetContainer();
            csc.Deserialize(serializedChanges, formatter);

            Assert.That(csc.AddedObjects.Count == 1);

            pm = new PersistenceManager();              // we need a new pm here which get's us id's beginning from -1
            pm.MergeObjectContainer(csc);
            // Now we should have a created object in the cache
            Assert.AreEqual(true, pm.HasChanges);
            m = (Mitarbeiter)pm.FindObject(typeof(Mitarbeiter), -1);
            Assert.That(m.NDOObjectState == NDOObjectState.Created);

            // Save and Reload
            pm.Save();
            Assert.That(m.NDOObjectState == NDOObjectState.Persistent);
            pm.UnloadCache();
            var l = pm.Objects <Mitarbeiter>().ResultTable;

            Assert.AreEqual(2, l.Count);
            Assert.That(l.Any(m1 => m1.Vorname == "Mirko" && m1.Nachname == "Matytschak"));
            Assert.That(l.Any(m1 => m1.Vorname == "Hans" && m1.Nachname == "Müller"));
        }
Esempio n. 3
0
        public void ObjectContainerIsSerializable(Type formatterType)
        {
            IFormatter formatter = (IFormatter)Activator.CreateInstance(formatterType);

            var m = CreateMitarbeiter();

            pm.MakePersistent(m);
            pm.Save();
            var oc = pm.GetObjectContainer();

            Assert.That(Object.ReferenceEquals(m, oc.RootObjects[0]));

            oc.Formatter = formatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            var oc2 = pm.GetObjectContainer();

            Assert.AreEqual(0, oc2.RootObjects.Count);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, formatter);
            pm.MergeObjectContainer(oc);

            oc2 = pm.GetObjectContainer();
            Assert.AreEqual(1, oc2.RootObjects.Count);

            m = (Mitarbeiter)oc2.RootObjects[0];
            Assert.AreEqual("Mirko", m.Vorname);
            Assert.AreEqual(NDOObjectState.Persistent, m.NDOObjectState);
        }
Esempio n. 4
0
        public void ChangeRelationWithExistingObject(Type formatterType)
        {
            // Create object and serialize it
            IFormatter formatter = (IFormatter)Activator.CreateInstance(formatterType);

            var r = new Reise {
                Zweck = "NDO"
            };

            pm.MakePersistent(r);
            var l = new Land("Germany");

            pm.MakePersistent(l);
            pm.Save();
            var oc = pm.GetObjectContainer();

            oc.Formatter = formatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            // Merge object in to an OfflinePersistenceManager and change it
            OfflinePersistenceManager opm = new OfflinePersistenceManager(pm.NDOMapping.FileName);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, formatter);

            opm.MergeObjectContainer(oc);

            var newOc = oc.RootObjects.Cast <IPersistenceCapable>();
            var r2    = (Reise)newOc.FirstOrDefault(pc => pc is Reise);
            var l2    = (Land)newOc.FirstOrDefault(pc => pc is Land);;

            Assert.AreEqual(NDOObjectState.Persistent, r2.NDOObjectState);
            Assert.AreEqual(NDOObjectState.Persistent, l2.NDOObjectState);
            r2.LandHinzufügen(l2);
            Assert.AreEqual(NDOObjectState.PersistentDirty, r2.NDOObjectState);

            // Create a ChangeSetContainer and serialize it
            var csc = opm.GetChangeSet();

            csc.Formatter = formatter;
            string serializedChanges = csc.MarshalingString;

            // Merge the changes back to pm
            csc = new ChangeSetContainer();
            csc.Deserialize(serializedChanges, formatter);
            r = (Reise)csc.ChangedObjects.Cast <IPersistenceCapable>().FirstOrDefault(pc => pc is Reise);
            pm.MergeObjectContainer(csc);
            r2 = (Reise)pm.FindObject(r.NDOObjectId);
            Assert.AreEqual(NDOObjectState.PersistentDirty, r2.NDOObjectState);
            Assert.AreEqual(1, r2.Länder.Count);

            // Save and Reload
            pm.Save();
            pm.UnloadCache();
            r = pm.Objects <Reise>().Single();
            Assert.AreEqual(1, r2.Länder.Count);
        }
Esempio n. 5
0
        public void ChangeRelationWithNewObject(Type formatterType)
        {
            // Create object and serialize it
            IFormatter formatter = (IFormatter)Activator.CreateInstance(formatterType);

            var m = CreateMitarbeiter();

            pm.MakePersistent(m);
            pm.Save();
            var oc = pm.GetObjectContainer();

            oc.Formatter = formatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            // Merge object in to an OfflinePersistenceManager and change it
            OfflinePersistenceManager opm = new OfflinePersistenceManager(pm.NDOMapping.FileName);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, formatter);

            opm.MergeObjectContainer(oc);

            var m2 = (Mitarbeiter)oc.RootObjects[0];

            Assert.AreEqual(NDOObjectState.Persistent, m2.NDOObjectState);
            m2.Hinzufuegen(new Reise()
            {
                Zweck = "NDO"
            });
            Assert.AreEqual(NDOObjectState.PersistentDirty, m2.NDOObjectState);

            // Create a ChangeSetContainer and serialize it
            var csc = opm.GetChangeSet();

            csc.Formatter = formatter;
            string serializedChanges = csc.MarshalingString;

            // Merge the changes back to pm
            csc = new ChangeSetContainer();
            csc.Deserialize(serializedChanges, formatter);
            m = (Mitarbeiter)csc.ChangedObjects[0];
            pm.MergeObjectContainer(csc);
            m2 = (Mitarbeiter)pm.FindObject(m.NDOObjectId);
            Assert.AreEqual(NDOObjectState.PersistentDirty, m2.NDOObjectState);
            Assert.AreEqual(1, m2.Reisen.Count);

            // Save and Reload
            pm.Save();
            pm.UnloadCache();
            m = pm.Objects <Mitarbeiter>().Single();
            Assert.AreEqual(1, m2.Reisen.Count);
        }
Esempio n. 6
0
        public void CompleteTurnaroundWithChangeSetContainer()
        {
            // Create object and serialize it
            var binaryFormatter = new BinaryFormatter();

            pm.MakePersistent(m);
            pm.Save();
            var oc = pm.GetObjectContainer();

            oc.Formatter = binaryFormatter;
            string serialized = oc.MarshalingString;

            pm.UnloadCache();

            // Merge object in to an OfflinePersistenceManager and change it
            OfflinePersistenceManager opm = new OfflinePersistenceManager(pm.NDOMapping.FileName);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, binaryFormatter);

            opm.MergeObjectContainer(oc);

            var m2 = (Mitarbeiter)oc.RootObjects[0];

            Assert.AreEqual(NDOObjectState.Persistent, m2.NDOObjectState);
            m2.Vorname = "Hans";
            Assert.AreEqual(NDOObjectState.PersistentDirty, m2.NDOObjectState);

            // Create a ChangeSetContainer and serialize it
            var csc = opm.GetChangeSet();

            csc.Formatter = binaryFormatter;
            string serializedChanges = csc.MarshalingString;

            // Merge the changes back to pm
            csc = new ChangeSetContainer();
            csc.Deserialize(serializedChanges, binaryFormatter);
            m = (Mitarbeiter)csc.ChangedObjects[0];
            pm.MergeObjectContainer(csc);
            m2 = (Mitarbeiter)pm.FindObject(m.NDOObjectId);
            Assert.AreEqual(NDOObjectState.PersistentDirty, m2.NDOObjectState);
            Assert.AreEqual("Hans", m.Vorname);

            // Save and Reload
            pm.Save();
            pm.UnloadCache();
            m = pm.Objects <Mitarbeiter>().Single();
            Assert.AreEqual("Hans", m.Vorname);
        }
Esempio n. 7
0
        public void CanDeserializeObjectContainer(bool serializeAddress)
        {
            ObjectContainer oc   = new ObjectContainer();
            string          json = CreateObjectContainer(serializeAddress);

            this.pm = new PersistenceManager();
            oc.Deserialize(json, new NdoJsonFormatter(this.pm));
            Assert.AreEqual(1, oc.RootObjects.Count);
            Assert.That(oc.RootObjects[0] is Mitarbeiter);
            Mitarbeiter m = (Mitarbeiter)oc.RootObjects[0];

            Assert.AreEqual("Mirko", m.Vorname);
            Assert.AreEqual(1, m.Reisen.Count);
            if (serializeAddress)
            {
                Assert.NotNull(m.Adresse);
                Assert.AreEqual("D", m.Adresse.Lkz);
            }
            else
            {
                Assert.IsNull(m.Adresse);
            }
        }
Esempio n. 8
0
        public void ObjectContainerSerializesRelations()
        {
            var binaryFormatter       = new BinaryFormatter();
            var serializationIterator = new NDO.SerializationIterator(r => r.ReferencedType == typeof(Reise));

            pm.MakePersistent(m);
            m.Hinzufuegen(new Reise()
            {
                Zweck = "ADC"
            });
            pm.Save();

            var oc = pm.GetObjectContainer();

            bool found = false;

            foreach (object o in oc.RootObjects)
            {
                if (Object.ReferenceEquals(m, o))
                {
                    found = true;
                }
            }
            Assert.That(found);

            oc.SerialisationIterator = serializationIterator;
            oc.Formatter             = binaryFormatter;
            string serialized = oc.MarshalingString;

            Assert.AreEqual(2, oc.RootObjects.Count);

            pm.UnloadCache();

            var oc2 = pm.GetObjectContainer();

            Assert.AreEqual(0, oc2.RootObjects.Count);

            oc = new ObjectContainer();
            oc.Deserialize(serialized, binaryFormatter);
            pm.MergeObjectContainer(oc);

            oc2 = pm.GetObjectContainer();
            Assert.AreEqual(2, oc2.RootObjects.Count);

            Reise       r2 = null;
            Mitarbeiter m2 = null;

            foreach (object o in oc2.RootObjects)
            {
                if (o is Reise)
                {
                    r2 = (Reise)o;
                }
                if (o is Mitarbeiter)
                {
                    m2 = (Mitarbeiter)o;
                }
            }

            Assert.NotNull(r2);
            Assert.NotNull(m2);

            Assert.AreEqual("Mirko", m2.Vorname);
            Assert.AreEqual(NDOObjectState.Persistent, m2.NDOObjectState);

            Assert.AreEqual("ADC", r2.Zweck);
            Assert.AreEqual(NDOObjectState.Persistent, r2.NDOObjectState);
        }