Beispiel #1
0
        public void ExpressionMapper_WorksWithJustDefaultMappings()
        {
            var foo = new Foo()
            {
                A = 5,
                B = 10
            };

            var mapper = new ExpressionMapper <Foo, Bar>(
                new ExpressionMappingComponents <Foo, Bar>(
                    defaultMappings: (Foo source) => new Bar()
            {
                A = source.A,
                B = source.B,
            }));

            var @new = mapper.Map(foo);

            Assert.AreEqual(@new.A, 5);
            Assert.AreEqual(@new.B, 10);
            Assert.AreEqual(@new.C, 0);
            Assert.AreEqual(@new.E, null);

            var existing = new Bar();

            mapper.Map(foo, existing);
            Assert.AreEqual(existing.A, 5);
            Assert.AreEqual(existing.B, 10);
            Assert.AreEqual(existing.C, 0);
            Assert.AreEqual(existing.E, null);
        }
Beispiel #2
0
        public void ExpressionMapper_MappingsCanBeNested()
        {
            var foo1 = new Foo1()
            {
                F   = 20,
                Foo = new Foo()
                {
                    A = 5,
                    B = 10
                }
            };

            var mappingsNested = new ExpressionMappingComponents <Foo, Bar>(
                defaultMappings: (Foo source) => new Bar()
            {
                A = source.A,
                B = source.B,
            },
                customMappings: (source) => new Bar()
            {
                C = 15
            },
                targetIgnoredProperties: new IgnoreList <Bar>(
                    x => x.A
                    ));

            var FooToBarExpression = mappingsNested.CombinedMappingsWithConstructor;

            var mapper = new ExpressionMapper <Foo1, Bar1>(
                new ExpressionMappingComponents <Foo1, Bar1>(
                    defaultMappings: (Foo1 source) => new Bar1()
            {
                F   = source.F,
                Bar = FooToBarExpression.Invoke(source.Foo),
            }));

            var @new = mapper.Map(foo1);

            Assert.AreEqual(@new.F, 20);
            Assert.AreEqual(@new.Bar.A, 5);
            Assert.AreEqual(@new.Bar.B, 10);
            Assert.AreEqual(@new.Bar.C, 15);
            Assert.AreEqual(@new.Bar.E, null);

            var existing = new Bar1();

            mapper.Map(foo1, existing);
            Assert.AreEqual(existing.F, 20);
            Assert.AreEqual(existing.Bar.A, 5);
            Assert.AreEqual(existing.Bar.B, 10);
            Assert.AreEqual(existing.Bar.C, 15);
            Assert.AreEqual(existing.Bar.E, null);
        }
Beispiel #3
0
        public void ExpressionMapTest()
        {
            var date         = DateTime.Now;
            var clonnedClass = new MappedClass
            {
                DateTimeProp = date,
                IntProp      = 5,
                StringProp   = "qwe"
            };

            var destClass = new DestClass
            {
                DateTimeProp = date,
                IntProp      = 5,
                StringProp   = "qwe"
            };

            var mapper = new ExpressionMapper();

            mapper.Register <MappedClass, DestClass>();

            var mapped = mapper.Map <MappedClass, DestClass>(clonnedClass);

            mapped.Should().BeEquivalentTo(destClass);

            Assert.Pass();
        }
Beispiel #4
0
        public void Not_Support_Enumerable_Test()
        {
            Assert.Throws <NotSupportedException>(() =>
            {
                ExpressionMapper.Map <IEnumerable <POCO>, IEnumerable <POCO> >(new List <POCO>()
                {
                    new POCO()
                    {
                        Hello = "World"
                    }
                });
            });

            Assert.Throws <NotSupportedException>(() =>
            {
                ExpressionMapper.Map <List <POCO>, List <POCO> >(new List <POCO>()
                {
                    new POCO()
                    {
                        Hello = "World"
                    }
                });
            });

            Assert.Throws <NotSupportedException>(() =>
            {
                ExpressionMapper.Map <POCO[], POCO[]>(new[] { new POCO()
                                                              {
                                                                  Hello = "World"
                                                              } });
            });
        }
Beispiel #5
0
        public void TestPropertyToPropertyBinary()
        {
            var sut = new ExpressionMapper(new FooBarMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Id > 3 || _.Id < -1);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo()).Should().BeFalse();
        }
Beispiel #6
0
        public void TestEmptyFilter()
        {
            var sut = new ExpressionMapper(new FooBarMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => true);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo()).Should().BeTrue();
        }
Beispiel #7
0
        public void TestPropertyToPropertyAccess()
        {
            var sut = new ExpressionMapper(new FooBarMap());

            var exp = sut.Map <Bar, Foo, int>(_ => _.Id);

            exp.Compile()(new Foo
            {
                Id = 10
            }).Should().Be(10);
        }
Beispiel #8
0
        public void TestPropertyToPropertyWithConversionExtensionMethodCall()
        {
            var sut = new ExpressionMapper(new FooBarConversionMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Names.Any());

            exp.Should().NotBeNull();

            exp.Compile()(new Foo {
                Ids = new[] { 0 }
            }).Should().BeTrue();
        }
Beispiel #9
0
        public void TestPropertyToPropertyExtensionMethodCall()
        {
            var sut = new ExpressionMapper(new FooBarMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Ids.Any(id => id > 5));

            exp.Should().NotBeNull();

            exp.Compile()(new Foo {
                Ids = new[] { 1, 2, 10 }
            }).Should().BeTrue();
        }
Beispiel #10
0
        public void TestPropertyToPropertyWithConversionBinary()
        {
            var sut = new ExpressionMapper(new FooBarConversionMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Name == "3");

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Id = 3
            }).Should().BeTrue();
        }
Beispiel #11
0
        public void TestPropertyToPropertyMethodCall()
        {
            var sut = new ExpressionMapper(new FooBarMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Name.Length > 3);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Name = "asdf"
            }).Should().BeTrue();
        }
Beispiel #12
0
        public void TestPropertyToPropertyWithConversionAccess()
        {
            var sut = new ExpressionMapper(new FooBarConversionMap());

            var exp = sut.Map <Bar, string, Foo, int>(_ => _.Name);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Id = 3
            }).Should().Be(3);
        }
Beispiel #13
0
        public void Mapping_Action_Test()
        {
            var time = DateTime.Now;
            var list = new List <POCO>()
            {
                new POCO()
                {
                    Hello = "World"
                }
            };

            var sourceObj = new SourceType()
            {
                Int                               = 10,
                Long                              = long.MaxValue,
                String                            = "String",
                Time                              = time,
                Nullable                          = null,
                NullableTime                      = null,
                NullableHasValue                  = 100,
                NullableTimeHasValue              = time,
                List                              = list,
                NonNullableToNullable             = 100,
                NullableToNonNullable             = null,
                NullableToNonNullableHasValue     = 111,
                NonNullableToNullableTime         = time,
                NullableToNonNullableTime         = null,
                NullableToNonNullableTimeHasValue = time
            };

            var targetObj = new TargetType();

            ExpressionMapper.Map(sourceObj, targetObj);
            Assert.NotNull(targetObj);
            Assert.Equal(10, targetObj.Int);
            Assert.Equal(long.MaxValue, targetObj.Long);
            Assert.Equal("String", targetObj.String);
            Assert.Equal(time, targetObj.Time);
            Assert.Null(targetObj.Nullable);
            Assert.Null(targetObj.NullableTime);
            Assert.Equal(100, targetObj.NullableHasValue);
            Assert.Equal(time, targetObj.NullableTimeHasValue);
            Assert.Equal(list, targetObj.List);
            Assert.Null(targetObj.NullList);
            Assert.Equal(100, targetObj.NonNullableToNullable);
            Assert.Equal(default(int), targetObj.NullableToNonNullable);
            Assert.Equal(111, targetObj.NullableToNonNullableHasValue);
            Assert.Equal(time, targetObj.NonNullableToNullableTime);
            Assert.Equal(default(DateTime), targetObj.NullableToNonNullableTime);
            Assert.Equal(time, targetObj.NullableToNonNullableTimeHasValue);
        }
Beispiel #14
0
        public void ExpressionMapper_Works()
        {
            var foo = new Foo()
            {
                A = 5,
                B = 10
            };

            var mapper = new ExpressionMapper <Foo, Bar>(
                new ExpressionMappingComponents <Foo, Bar>(
                    defaultMappings: (Foo source) => new Bar()
            {
                A = source.A,
                B = source.B,
            },
                    customMappings: (source) => new Bar()
            {
                C = 15
            },
                    targetIgnoredProperties: new IgnoreList <Bar>(
                        x => x.A
                        )));

            var @new = mapper.Map(foo);

            Assert.AreEqual(@new.A, 5);
            Assert.AreEqual(@new.B, 10);
            Assert.AreEqual(@new.C, 15);
            Assert.AreEqual(@new.E, null);

            var existing = new Bar();

            mapper.Map(foo, existing);
            Assert.AreEqual(existing.A, 5);
            Assert.AreEqual(existing.B, 10);
            Assert.AreEqual(existing.C, 15);
            Assert.AreEqual(existing.E, null);
        }
Beispiel #15
0
        public void TestComponentToComponentBinary()
        {
            var sut = new ExpressionMapper(new FooBarMap(), new BazQuxMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Component.Id > 3);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Component = new Baz
                {
                    Id = 4
                }
            }).Should().BeTrue();
        }
Beispiel #16
0
        public void TestExpressionToProperty()
        {
            var sut = new ExpressionMapper(new FooBarExpressionMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Name == "123");

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Component = new Baz
                {
                    Id = 123
                }
            }).Should().BeTrue();
        }
Beispiel #17
0
        public void TestComponentToComponentMethodCall()
        {
            var sut = new ExpressionMapper(new FooBarMap(), new BazQuxMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Component.Id.ToString().Length > 3);

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Component = new Baz
                {
                    Id = 4000
                }
            }).Should().BeTrue();
        }
Beispiel #18
0
        public void TestComponentCollectionBinary()
        {
            var sut = new ExpressionMapper(new FooBarMap(), new BazQuxMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Quxes.Any(q => q.Id > 10));

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Bazes = new[]
                {
                    new Baz
                    {
                        Id = 20
                    }
                }
            }).Should().BeTrue();
        }
Beispiel #19
0
        public void TestCollectionMethodCall()
        {
            var sut = new ExpressionMapper(new FooBarCollectionMap());

            var exp = sut.Map <Bar, Foo, bool>(_ => _.Names.Any(n => n.Length > 1));

            exp.Should().NotBeNull();

            exp.Compile()(new Foo
            {
                Bazes = new[]
                {
                    new Baz
                    {
                        Id = 20
                    }
                }
            }).Should().BeTrue();
        }
Beispiel #20
0
 public TDal GetByPredicate(Expression <Func <TDal, bool> > predicate)
 {
     return(_mapper.ToDal(_dbSet.FirstOrDefault(ExpressionMapper <TDal, TEntity, bool> .Map(predicate))));
 }