public static void GerarModelo2(int tamanho) { var mapper = FixedLengthTypeMapper.DefineDynamic(typeof(Person)); mapper.StringProperty("Name", tamanho).ColumnName("Name"); mapper.Int32Property("IQ", tamanho).ColumnName("IQ"); mapper.DateTimeProperty("BirthDate", 10).ColumnName("BirthDate").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.DecimalProperty("TopSpeed", 10).ColumnName("TopSpeed"); var people = new[] { new Person() { Name = "John", IQ = null, BirthDate = new DateTime(1954, 10, 29), TopSpeed = 3.4m }, new Person() { Name = "Susan", IQ = 132, BirthDate = new DateTime(1984, 3, 15), TopSpeed = 10.1m } }; StringWriter writer = new StringWriter(); mapper.Write(writer, people); string result = writer.ToString(); using (StreamWriter writer2 = new StreamWriter($@"D:\teste\GerarModelo2_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt")) { writer2.Write(result); } }
public void TestRuntimeTypeDefinition() { var mapper = FixedLengthTypeMapper.DefineDynamic(typeof(Person)); mapper.StringProperty("Name", 10).ColumnName("Name"); mapper.Int32Property("IQ", 3).ColumnName("IQ"); mapper.DateTimeProperty("BirthDate", 10).ColumnName("BirthDate").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.DecimalProperty("TopSpeed", 10).ColumnName("TopSpeed"); var people = new[] { new Person() { Name = "John", IQ = null, BirthDate = new DateTime(1954, 10, 29), TopSpeed = 3.4m }, new Person() { Name = "Susan", IQ = 132, BirthDate = new DateTime(1984, 3, 15), TopSpeed = 10.1m } }; StringWriter writer = new StringWriter(); mapper.Write(writer, people); string result = writer.ToString(); StringReader reader = new StringReader(result); var parsed = mapper.Read(reader).ToArray(); Assert.AreEqual(2, parsed.Length); Assert.IsInstanceOfType(parsed[0], typeof(Person)); Assert.IsInstanceOfType(parsed[1], typeof(Person)); assertEqual(people[0], (Person)parsed[0]); assertEqual(people[1], (Person)parsed[1]); }
public void TestRuntimeTypeDefinition_ReaderWriter() { var mapper = FixedLengthTypeMapper.DefineDynamic(typeof(Person)); mapper.StringProperty("Name", 10).ColumnName("Name"); mapper.Int32Property("IQ", 3).ColumnName("IQ"); mapper.DateTimeProperty("BirthDate", 10).ColumnName("BirthDate").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.DecimalProperty("TopSpeed", 10).ColumnName("TopSpeed"); var people = new[] { new Person() { Name = "John", IQ = null, BirthDate = new DateTime(1954, 10, 29), TopSpeed = 3.4m }, new Person() { Name = "Susan", IQ = 132, BirthDate = new DateTime(1984, 3, 15), TopSpeed = 10.1m } }; StringWriter writer = new StringWriter(); var entityWriter = mapper.GetWriter(writer); foreach (var person in people) { entityWriter.Write(person); } string result = writer.ToString(); StringReader reader = new StringReader(result); var entityReader = mapper.GetReader(reader); var parsed = new List <object>(); while (entityReader.Read()) { parsed.Add(entityReader.Current); } Assert.AreEqual(2, parsed.Count); Assert.IsInstanceOfType(parsed[0], typeof(Person)); Assert.IsInstanceOfType(parsed[1], typeof(Person)); assertEqual(people[0], (Person)parsed[0]); assertEqual(people[1], (Person)parsed[1]); }
public void TestRoundTrip_FixedLength_Dynamic() { var mapper = FixedLengthTypeMapper.DefineDynamic(typeof(Person), () => new Person()); mapper.Int32Property("Id", 10).ColumnName("Id"); mapper.StringProperty("Name", 25).ColumnName("Name"); mapper.StringProperty("Address1.Street", 50).ColumnName("Street"); mapper.StringProperty("Address1.City", 50).ColumnName("City"); mapper.StringProperty("Address1.State", 2).ColumnName("State"); mapper.StringProperty("Address1.Zip", 5).ColumnName("Zip"); mapper.BooleanProperty("IsActive", 5).ColumnName("IsActive"); mapper.DateTimeProperty("CreatedOn", 10).ColumnName("CreatedOn").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.UseFactory(typeof(Address), () => new Address()); var expected = new Person() { Id = 123, Name = "Bob", Address1 = new Address() { Street = "Test Street 1", City = "Test City", State = "PA", Zip = "55555" }, IsActive = true, CreatedOn = new DateTime(2017, 11, 05) }; StringWriter writer = new StringWriter(); mapper.Write(writer, new[] { expected }); StringReader reader = new StringReader(writer.ToString()); var results = mapper.Read(reader).ToArray(); Assert.Equal(1, results.Length); var result = results[0]; Assert.IsType <Person>(result); assertEqual(expected, (Person)result); assertEqual(expected.Address1, ((Person)result).Address1); }