public void ComplexTest()
        {
            MemoryStream stream = new MemoryStream();

            int firstCompanyId;

            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    Company company = new Company {
                        Name = "OpenTransaction lines"
                    };
                    firstCompanyId = company.Id;
                    transactionScope.Complete();
                }
            }

            using (var session = Domain.OpenSession()) {
                using (session.OpenTransaction()) {
                    object[] array;
                    Company  existingCompany = (Company)session.Query.SingleOrDefault(Key.Create(Domain, typeof(Company), firstCompanyId)); //session.Query.All<Company>().First();


                    Company company = new Company {
                        Name = "Region mobile"
                    };
                    Emploee mike = new Emploee {
                        Name = "Mike", Company = company
                    };
                    Emploee alex = new Emploee {
                        Name = "Alex", Company = company
                    };
                    Emploee jef = new Emploee {
                        Name = "Jef", Company = existingCompany
                    };
                    company.Head = alex;

                    array = new object[] { existingCompany, company, alex, jef };
                    session.Validate();


                    SerializationContext context =
                        new SerializationContext(entity => entity == existingCompany ? SerializationKind.ByReference : SerializationKind.ByValue);

                    using (context.Activate()) {
                        formatter.Serialize(stream, array);
                    }
                    // Rollback
                }
            }

            using (var session = Domain.OpenSession()) {
                using (var transactionScope = session.OpenTransaction()) {
                    object[] array;
                    stream.Position = 0;

                    var deserializationContext = new DeserializationContext();

                    using (deserializationContext.Activate()) {
                        array = (object [])formatter.Deserialize(stream);
                    }

                    Company oldCompany = (Company)array[0];
                    Company newCompany = (Company)array[1];
                    Emploee alex       = (Emploee)array[2];
                    Emploee jef        = (Emploee)array[3];

                    Assert.AreEqual(firstCompanyId, oldCompany.Id);
                    Assert.AreEqual("Alex", alex.Name);
                    Assert.AreEqual("Jef", jef.Name);

                    Assert.AreEqual(oldCompany, jef.Company);
                    Assert.AreEqual(newCompany, alex.Company);
                    Assert.AreEqual(alex, newCompany.Head);
                }
            }
        }
        public void ReferencedKeysTest()
        {
            Stream stream = new MemoryStream();

            using (var session = Domain.OpenSession()) {
                Country russia;

                using (var transactionScope = session.OpenTransaction()) {
                    russia = new Country("Russia");
                    transactionScope.Complete();
                }

                using (session.OpenTransaction()) {
                    Country china = new Country("China");

                    City moscow       = new City(russia, "Moscow");
                    City ekaterinburg = new City(russia, "Ekaterinburg");
                    City hongKong     = new City(china, "Hong Kong");
                    City beijing      = new City(china, "Beijing");
                    City guangzhou    = new City(china, "Guangzhou");

//          china.Capital = beijing;
//          russia.Capital = moscow;

                    City[] cities = new[] { ekaterinburg, moscow, hongKong, guangzhou, beijing };

                    var serializationContext = new SerializationContext(
                        entity => entity == russia ? SerializationKind.ByReference : SerializationKind.ByValue);

                    using (serializationContext.Activate()) {
                        formatter.Serialize(stream, cities);
                    }
                    // Rollback
                }
            }

            using (var session = Domain.OpenSession()) {
                using (session.OpenTransaction()) {
                    var deserializationContext = new DeserializationContext();

                    City[] cities;

                    using (deserializationContext.Activate()) {
                        stream.Position = 0;
                        cities          = (City[])formatter.Deserialize(stream);
                    }

                    City ekaterinburg = cities[0];
                    City moscow       = cities[1];
                    City hongKong     = cities[2];
                    City guangzhou    = cities[3];

                    Country russia = ekaterinburg.Country;
                    Country china  = guangzhou.Country;

                    Assert.IsNotNull(russia);
                    Assert.IsNotNull(china);

                    Assert.AreEqual("Russia", russia.Name);
                    Assert.AreEqual("China", china.Name);

                    Assert.AreEqual("Ekaterinburg", ekaterinburg.Name);
                    Assert.AreEqual("Moscow", moscow.Name);
                    Assert.AreEqual(russia, moscow.Country);
                    Assert.AreEqual("Hong Kong", hongKong.Name);
                    Assert.AreEqual(china, hongKong.Country);

//          Assert.IsNull(russia.Capital); // russia was serialized by reference, not by value
//
//          Assert.AreEqual(china.Capital.Name, "Beijing");
                }
            }
        }