Esempio n. 1
0
        public static void ListShallowCopyDemo()
        {
            List <PersonA> personList = new List <PersonA>()
            {
                new PersonA()
                {
                    Name = "PersonA", Age = 10, ClassA = new A()
                    {
                        TestProperty = "AProperty"
                    }
                },
                new PersonA()
                {
                    Name = "PersonA2", Age = 20, ClassA = new A()
                    {
                        TestProperty = "AProperty2"
                    }
                }
            };

            // 下面2种方式实现的都是浅拷贝
            List <PersonA> personsCopy = new List <PersonA>(personList);

            PersonA[] personCopy2 = new PersonA[2];
            personList.CopyTo(personCopy2);

            // 由于实现的是浅拷贝,所以改变一个对象的值,其他2个对象的值都会发生改变,因为它们都是使用的同一份实体,即它们指向内存中同一个地址
            personsCopy.First().ClassA.TestProperty = "AProperty3";
            WriteLog(string.Format("personCopy2.First().ClassA.TestProperty is {0}", personCopy2.First().ClassA.TestProperty));
            WriteLog(string.Format("personList.First().ClassA.TestProperty is {0}", personList.First().ClassA.TestProperty));
            WriteLog(string.Format("personsCopy.First().ClassA.TestProperty is {0}", personsCopy.First().ClassA.TestProperty));
            Console.Read();
        }
 public void ByteArray_SerializePersonA()
 {
     AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
     PersonA personA = new PersonA(@"Bob", @"Bones", addressA);
     byte[] data = SerializationHelper.SerializeToBytes(personA);
     Assert.IsNotNull(data);
     Assert.IsTrue(data.Length > 0);
     PersonA deserializedPersonA = SerializationHelper.DeserializeFromBytes<PersonA>(data);
     Assert.AreEqual(personA, deserializedPersonA);
 }
 public void XmlSerialization_SerializePersonA()
 {
     AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
     PersonA personA = new PersonA(@"Bob", @"Bones", addressA);
     string xmlData = SerializationHelper.SerializeToXmlString(personA);
     Assert.IsNotNull(xmlData);
     Assert.IsTrue(xmlData.Length > 0);
     PersonA deserializedPersonA = SerializationHelper.DeserializeFromXmlString<PersonA>(xmlData);
     Assert.AreEqual(personA, deserializedPersonA);
 }
 public void XmlSerialization_SerializePersonAUsingAnInterface()
 {
     IAddress address = new AddressA(@"CornerStreet", @"00501", @"New york");
     IPerson person = new PersonA(@"Bob", @"Bones", address);
     string xmlData = SerializationHelper.SerializeToXmlString(person);
     Assert.IsNotNull(xmlData);
     Assert.IsTrue(xmlData.Length > 0);
     IPerson deserializedPerson = SerializationHelper.DeserializeFromXmlString<IPerson>(xmlData);
     Assert.AreEqual(person, deserializedPerson);
     Assert.IsInstanceOf<PersonA>(person);
 }
 public void ByteArray_SerializePersonAUsingAnInterface()
 {
     IAddress address = new AddressA(@"CornerStreet", @"00501", @"New york");
     IPerson person = new PersonA(@"Bob", @"Bones", address);
     byte[] data = SerializationHelper.SerializeToBytes(person);
     Assert.IsNotNull(data);
     Assert.IsTrue(data.Length > 0);
     IPerson deserializedPerson = SerializationHelper.DeserializeFromBytes<IPerson>(data);
     Assert.AreEqual(person, deserializedPerson);
     Assert.IsInstanceOf<PersonA>(person);
 }
Esempio n. 6
0
        public void DynamicVisitorPatternTest()
        {
            IVisitable personA = new PersonA();
            IVisitable personB = new PersonB();

            IVisitor toStrVisitor     = new ToStringVisitor();
            IVisitor toElementVisitor = new ToElementPersonVisitor();

            _testOutputHelper.WriteLine(personA.Accept(toElementVisitor));
            _testOutputHelper.WriteLine(personA.Accept(toStrVisitor));
            _testOutputHelper.WriteLine(personB.Accept(toElementVisitor));
            _testOutputHelper.WriteLine(personB.Accept(toStrVisitor));
        }
Esempio n. 7
0
		public static void Main(string[] args)
		{
			//Con//using Object Initializer
			var person1 = new PersonA
			{
				Id = 21,
				FirstName = "Bernat",
				SecondName = "Ferragut",
			};

			var person2 = new PersonA
			{
				Id = 31,
				FirstName = "Laurent",
				SecondName = "Ferragut",
			};
	}
Esempio n. 8
0
        public void DifferentTablesUseSamePkGenerator_InsertSingleItem()
        {
            using TestDatabase db = CreateTestDatabase();

            IDbSet <PersonA> setA = db.Query <PersonA>().AsDbSet();
            var personA1          = new PersonA {
                Name = "Alice A"
            };
            var personA2 = new PersonA {
                Name = "Bob A"
            };
            var personA3 = new PersonA {
                Name = "Connor A"
            };

            IDbSet <PersonB> setB = db.Query <PersonB>().AsDbSet();
            var personB1          = new PersonB {
                Name = "Alice B"
            };
            var personB2 = new PersonB {
                Name = "Bob B"
            };
            var personB3 = new PersonB {
                Name = "Connor B"
            };

            InsertItems(setA, personA1);
            InsertItems(setB, personB1);
            InsertItems(setA, personA2);
            InsertItems(setB, personB2);
            InsertItems(setA, personA3);
            InsertItems(setB, personB3);

            personA1.IdA.Should().Be(1);
            personA2.IdA.Should().Be(3);
            personA3.IdA.Should().Be(5);
            personB1.Pk.Should().Be(2);
            personB2.Pk.Should().Be(4);
            personB3.Pk.Should().Be(6);
        }
Esempio n. 9
0
        public void DifferentTablesUseSamePkGenerator_InsertMultipleItems()
        {
            using TestDatabase db = CreateTestDatabase();

            IDbSet <PersonA> setA = db.Query <PersonA>().AsDbSet();
            var personA1          = new PersonA {
                Name = "Alice A"
            };
            var personA2 = new PersonA {
                Name = "Bob A"
            };
            var personA3 = new PersonA {
                Name = "Connor A"
            };
            var personA4 = new PersonA {
                Name = "Drew A"
            };
            var personA5 = new PersonA {
                Name = "Eve A"
            };
            var personA6 = new PersonA {
                Name = "Fiona A"
            };

            IDbSet <PersonB> setB = db.Query <PersonB>().AsDbSet();
            var personB1          = new PersonB {
                Name = "Alice B"
            };
            var personB2 = new PersonB {
                Name = "Bob B"
            };
            var personB3 = new PersonB {
                Name = "Connor B"
            };
            var personB4 = new PersonB {
                Name = "Drew B"
            };
            var personB5 = new PersonB {
                Name = "Eve B"
            };
            var personB6 = new PersonB {
                Name = "Fiona B"
            };

            InsertItems(setA, new[] { personA1, personA2, personA3 });
            InsertItems(setB, new[] { personB1, personB2, personB3 });
            InsertItems(setA, new[] { personA4, personA5, personA6 });
            InsertItems(setB, new[] { personB4, personB5, personB6 });

            personA1.IdA.Should().Be(1);
            personA2.IdA.Should().Be(2);
            personA3.IdA.Should().Be(3);
            personA4.IdA.Should().Be(7);
            personA5.IdA.Should().Be(8);
            personA6.IdA.Should().Be(9);

            personB1.Pk.Should().Be(4);
            personB2.Pk.Should().Be(5);
            personB3.Pk.Should().Be(6);
            personB4.Pk.Should().Be(10);
            personB5.Pk.Should().Be(11);
            personB6.Pk.Should().Be(12);
        }
Esempio n. 10
0
 public string Visit(PersonA visitable)
 {
     return($"{nameof(ToElementPersonVisitor)} Visited {nameof(PersonA)}");
 }
 public void Stream_SerializePersonBUsingAnInterface()
 {
     IAddress address = new AddressA(@"CornerStreet", @"00501", @"New york");
     IPerson partner = new PersonA(@"Kate", @"Moss", address);
     IPerson person = new PersonB(@"Bob", @"Bones", address, partner);
     using (Stream stream = new MemoryStream())
     {
         SerializationHelper.Serialize(stream, person);
         Assert.IsTrue(stream.Length > 0);
         stream.Position = 0;
         IPerson deserializedPerson = SerializationHelper.Deserialize<IPerson>(stream);
         Assert.AreEqual(person, deserializedPerson);
         Assert.IsInstanceOf<PersonA>(person);
     }
 }
 public void Stream_SerializePersonB()
 {
     AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
     PersonA partner = new PersonA(@"Kate", @"Moss", addressA);
     PersonA personB = new PersonB(@"Bob", @"Bones", addressA, partner);
     using (Stream stream = new MemoryStream())
     {
         SerializationHelper.Serialize(stream, personB);
         Assert.IsTrue(stream.Length > 0);
         stream.Position = 0;
         PersonA deserializedPersonB = SerializationHelper.Deserialize<PersonA>(stream);
         Assert.AreEqual(personB, deserializedPersonB);
     }
 }
 public void CompressedByteArray_SerializePersonB()
 {
     AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
     PersonA partner = new PersonA(@"Kate", @"Moss", addressA);
     PersonA personB = new PersonB(@"Bob", @"Bones", addressA, partner);
     byte[] data = SerializationHelper.SerializeToBytes(personB, true);
     Assert.IsNotNull(data);
     Assert.IsTrue(data.Length > 0);
     PersonB deserializedPersonB = SerializationHelper.DeserializeFromBytes<PersonB>(data, true);
     Assert.AreEqual(personB, deserializedPersonB);
 }
        public void CompressedStream_SerializePersonA()
        {
            AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
            PersonA personA = new PersonA(@"Bob", @"Bones", addressA);

            byte[] memory;

            using (MemoryStream stream = new MemoryStream())
            {
                // compressing stream only forced to flush when the stream is closed
                using (Stream compressed = Compression.CompressingStream(stream))
                {
                    SerializationHelper.Serialize(compressed, personA);
                    Assert.IsTrue(stream.Length > 0);
                }

                // copy only after the compressing stream has been closed
                memory = stream.ToArray();
            }

            PersonA deserializedPersonA;
            using (Stream stream = new MemoryStream(memory))
            using (Stream decompressed = Compression.DecompressingStream(stream))
            {
                deserializedPersonA = SerializationHelper.Deserialize<PersonA>(decompressed);
            }

            Assert.AreEqual(personA, deserializedPersonA);
        }
        public void CompressedStream_SerializePersonA()
        {
            AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
            PersonA personA = new PersonA(@"Bob", @"Bones", addressA);

            byte[] memory;

            using (MemoryStream stream = new MemoryStream())
            using (Stream compressed = Compression.CompressingStream(stream))
            {
                SerializationHelper.Serialize(compressed, personA);
                Assert.IsTrue(stream.Length > 0);
                memory = stream.GetBuffer();
            }

            PersonA deserializedPersonA;
            using (Stream stream = new MemoryStream(memory))
            using (Stream decompressed = Compression.DecompressingStream(stream))
            {
                deserializedPersonA = SerializationHelper.Deserialize<PersonA>(decompressed);
            }

            Assert.AreEqual(personA, deserializedPersonA);
        }
 public void CompressedByteArray_SerializePersonBUsingAnInterface()
 {
     IAddress address = new AddressA(@"CornerStreet", @"00501", @"New york");
     IPerson partner = new PersonA(@"Kate", @"Moss", address);
     IPerson person = new PersonB(@"Bob", @"Bones", address, partner);
     byte[] data = SerializationHelper.SerializeToBytes(person, true);
     Assert.IsNotNull(data);
     Assert.IsTrue(data.Length > 0);
     IPerson deserializedPerson = SerializationHelper.DeserializeFromBytes<IPerson>(data, true);
     Assert.AreEqual(person, deserializedPerson);
     Assert.IsInstanceOfType(person, typeof(PersonB));
 }