public void MaterializeByteArray() { var modelMapper = CreateModelMapperForBar(); DynamicMethodModelFactory factory = new DynamicMethodModelFactory(new DatabaseMapper(modelMapper)); var rows = new List <Dictionary <string, object> > { new Dictionary <string, object>() }; var image = new byte[] { 0x20, 0x20, 0x20 }; rows[0].Add("Prop1", 2); rows[0].Add("Prop2", 1); rows[0].Add("Prop4", 111); rows[0].Add("Image", image); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Bar>(data); var bar = fact(data); bar.Image.Should().BeEquivalentTo(image); }
public void MaterializeByteArrayInvalidCast() { var modelMapper = CreateModelMapperForBar(); DynamicMethodModelFactory factory = new DynamicMethodModelFactory(new DatabaseMapper(modelMapper)); var rows = new List <Dictionary <string, object> > { new Dictionary <string, object>() }; var image = new byte[] { 0x20, 0x20, 0x20 }; rows[0].Add("Prop1", 2); rows[0].Add("Prop2", 1); rows[0].Add("Prop4", image); // Bar.Prop4 => integer rows[0].Add("Image", image); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Bar>(data); Bar bar; Action act = () => bar = fact(data); act.Should().Throw <InvalidCastException>(); }
public void CallOnAfterMaterializeWhenModelImplementIMaterialize() { var modelMapper = CreateModelMapperForBar(); DynamicMethodModelFactory factory = new DynamicMethodModelFactory(new DatabaseMapper(modelMapper)); var rows = new List <Dictionary <string, object> > { new Dictionary <string, object>() }; rows[0].Add("Prop1", 2); rows[0].Add("Prop2", 1); rows[0].Add("Prop4", 111); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Bar>(data); var bar = fact(data); bar.Prop3.Should().Be(6); bar.Prop4.Should().Be(111); }
/// <summary> /// Static constructor for initializing default behaviours. /// </summary> static Database() { SqlServerQueryProviderFactory.Register(); DefaultModelMapper = new ConventionModelMapper(); DatabaseMapper = new DatabaseMapper(DefaultModelMapper); DefaultModelFactory = new DynamicMethodModelFactory(DatabaseMapper); }
/// <summary> /// Initializes a new instance of the <see cref="Database" /> class. /// </summary> /// <param name="activeConnection">The active connection.</param> /// <param name="modelMapper">Model mapper, which will be used for mapping Object to Relation database.</param> public Database(DbConnection activeConnection, IModelMapper modelMapper) { Check.NotNull(activeConnection, nameof(activeConnection)); Check.NotNull(modelMapper, nameof(modelMapper)); var databaseMapper = new DatabaseMapper(modelMapper); var defaultModelFactory = new DynamicMethodModelFactory(databaseMapper); Init(activeConnection, QueryProviderFactories.GetFactory(activeConnection), databaseMapper, defaultModelFactory); }
public void IgnoreNonExistingProperties() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); rows[0].Add("XXX", 3); using IDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); }
public void CreateFactoryWhichConvertNullableType() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.Age.Should().Be(18.5); }
public void CreateFactoryWhichKnowFillingObjectsWithEnums() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); rows[0].Add("PropertyEnum", 3); using IDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.PropertyEnum.Should().Be(TestEnum.Value3); }
public void IgnoreNonMapFields() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); rows[0].Add("Bar", 3); using IDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.Bar.Should().Be(0); }
public void CreateFactoryWhichUseConverter() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); rows[0].Add("PropertyEnumConv", "V2"); using IDataReader data = new InMemoryDataReader(rows); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.PropertyEnumConv.Should().Be(TestEnum.Value2); }
public void CreateFactoryWhichUseStandardTypeConverter() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); data.CurrentTypes[0] = typeof(double); data.CurrentValues[0] = 25.45; var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.Id.Should().Be(25); }
public void NonNullableTypeMaterializeToNullable() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); rows[0].Add("PropertyDateTimeNullable", new DateTime(2005, 1, 5)); using InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); data.CurrentTypes[7] = typeof(DateTime); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.PropertyDateTimeNullable.Should().Be(new DateTime(2005, 1, 5)); }
public void CreateFactoryWhichKnowFillingObjectsWithPrimitiveTypes() { DynamicMethodModelFactory factory = CreateFactory(); IDataReader data = new InMemoryDataReader(CreateData()); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.Id.Should().Be(1); foo.PropertyString.Should().Be("Hello"); foo.PropertyDouble.Should().Be(45.78); foo.PropertyDecimal.Should().Be(785.78M); foo.PropertyDateTime.Should().Be(new DateTime(1980, 7, 24)); foo.Is.Should().BeTrue(); foo.PropertyGuid.Should().Be(new Guid("ddc995d7-4dda-41ca-abab-7f45e651784a")); }
public void CreateFactoryWhichKnowFillingObjectsWithNullValues() { DynamicMethodModelFactory factory = CreateFactory(); var rows = CreateData(); InMemoryDataReader data = new InMemoryDataReader(rows); data.Read(); data.CurrentValues[1] = DBNull.Value; data.CurrentValues[2] = DBNull.Value; data.CurrentValues[6] = DBNull.Value; var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.PropertyString.Should().BeNull(); foo.PropertyDouble.Should().Be(0); foo.PropertyGuid.Should().BeEmpty(); }
public void MaterializeValueType() { DynamicMethodModelFactory factory = CreateFactory(); var reader = new ValueTypesReader <int>(new List <int>() { 2, 5, 9, 15, 3 }); var actual = new List <int>(); while (reader.Read()) { var fact = factory.GetFactory <int>(reader); actual.Add(fact(reader)); } actual.Should().BeEquivalentTo(new List <int>() { 2, 5, 9, 15, 3 }); }
public void CreateFactoryWhichKnowFillingObjectsWithPrimitiveTypes() { DynamicMethodModelFactory factory = CreateFactory(); using IDataReader data = new InMemoryDataReader(CreateData()); data.Read(); var fact = factory.GetFactory <Foo>(data); var foo = fact(data); foo.Id.Should().Be(1); foo.PropertyString.Should().Be("Hello"); foo.PropertyDouble.Should().Be(45.78); foo.PropertyDecimal.Should().Be(785.78M); foo.PropertyDateTime.Should().Be(new DateTime(1980, 7, 24)); foo.DateTimeOffset.Should().Be(new DateTimeOffset(1985, 9, 20, 10, 11, 22, 123, TimeSpan.FromHours(5))); foo.DateTimeOffsetNullable.Should().Be(new DateTimeOffset(1985, 9, 20, 10, 11, 22, 123, TimeSpan.FromHours(5))); foo.Float.Should().Be(12.8F); foo.FloatNullable.Should().Be(45.89F); foo.Is.Should().BeTrue(); foo.PropertyGuid.Should().Be(new Guid("ddc995d7-4dda-41ca-abab-7f45e651784a")); }
private DynamicMethodModelFactory CreateFactory(IModelMapper modelMapper) { DynamicMethodModelFactory factory = new DynamicMethodModelFactory(new DatabaseMapper(modelMapper)); return(factory); }