Ejemplo n.º 1
0
        public void BaseClassMappingAndInheritedClassMappingIsRespected()
        {
            var mapper = new MemberMapper();

              mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;

              mapper.CreateMap<SourceBase, DestinationBase>(customMapping: src => new DestinationBase
              {
            Test = src.ID
              });

              mapper.CreateMap<SecondSourceInherited, SecondDestinationInherited>(customMapping: src => new SecondDestinationInherited
              {
            Bar = src.Foo
              });

              var result = mapper.Map<SecondSourceInherited, SecondDestinationInherited>(new SecondSourceInherited
              {
            ID = 10,
            Foo = "test"
              });

              Assert.AreEqual(10, result.Test);
              Assert.AreEqual("test", result.Bar);
        }
        public void NestedPropertyWithCustomMappingWorks()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<Address, AddressDto>(src => new AddressDto
              {
            CountryName = src.Country.Name
              });

              var projection = mapper.Project<CustomerSingleAddress, CustomerSingleAddressDto>();

              var method = projection.Compile();

              var result = method(new CustomerSingleAddress
              {
            Address =
              new Address
              {
            Country = new Country
            {
              Name = "X"
            }
              }
              });

              Assert.AreEqual("X", result.Address.CountryName);
        }
Ejemplo n.º 3
0
        public void CustomMappingIsRespected()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<Customer, SimpleCustomerDto>(customMapping: c => new
              {
            FullName = c.FirstName + " " + c.LastName.ToString(),
            AddressLine = c.Address.Street + " " + c.Address.HouseNumber,
            EmailAddress = c.EmailAddress.Address
              });

              var customer = new Customer
              {
            FirstName = "Test",
            LastName = "Test",
            EmailAddress = new EmailAddress
            {
              Address = "*****@*****.**"
            },
            CustomerID = 1,
            Address = new Address
            {
              Street = "test",
              HouseNumber = "10",
              ZipCode = "1111AB",
              City = "test"
            }
              };

              var dto = mapper.Map<Customer, SimpleCustomerDto>(customer);
        }
Ejemplo n.º 4
0
        public void ClearMapCacheIsRespected()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>(customMapping: src => new
              {
            ID = src.ID * 10,
            Name = src.Name + src.Name
              });

              var source = new SourceType
              {
            ID = 10,
            Name = "x"
              };

              var result = mapper.Map<DestinationType>(source);

              Assert.AreEqual(100, result.ID);
              Assert.AreEqual("xx", result.Name);

              mapper.ClearMapCache();

              result = mapper.Map<DestinationType>(source);

              Assert.AreEqual(10, result.ID);
              Assert.AreEqual("x", result.Name);
        }
Ejemplo n.º 5
0
        public void ComplexTypeMappingRespectsExistingMapping()
        {
            var mapper = new MemberMapper();

              var proposed = mapper.CreateMap(typeof(ComplexSourceType), typeof(ComplexDestinationType),
              (s, p, option) =>
              {
            if (s.Name == "Name")
            {
              option.IgnoreMember();
            }
              });

              proposed.FinalizeMap();

              var source = new ComplexSourceType
              {
            ID = 5,
            Complex = new NestedSourceType
            {
              ID = 10,
              Name = "test"
            }
              };

              var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source);

              Assert.AreEqual(destination.ID, 5);
              Assert.IsNotNull(destination.Complex);
              Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name);
        }
Ejemplo n.º 6
0
        public void GetMapWorks()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>();

              Assert.IsNotNull(mapper.GetMap<SourceType, DestinationType>());
        }
Ejemplo n.º 7
0
        public void HasMapWorks()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>();

              Assert.IsTrue(mapper.HasMap<SourceType, DestinationType>());
        }
Ejemplo n.º 8
0
        public void InvokingMapWithoutParameterAsMapWithParameterThrows()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>();

              var result = mapper.Map(new SourceType(), new DestinationType(), 1);
        }
Ejemplo n.º 9
0
        public void MapForMapWithParameterThrowsForMapWithoutParameter()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>();

              mapper.Map<SourceType, DestinationType, int>(new SourceType(), 0);
        }
        //[TestMethod]
        public void AutoConvertWorks()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType>(src => new
            {
                Bar = src.Foo
            });
        }
Ejemplo n.º 11
0
        public void TypeMappingsAreNotCachedWhenRecursionIsDetected()
        {
            var mapper = new MemberMapper();

            mapper.Options.Cloning.MaxCloneDepth = null;

            mapper.CreateMap <ClassA, ClassA>();

            mapper.CreateMap <ClassB, ClassB>();

            var result = mapper.Map <ClassB, ClassB>(new ClassB
            {
                FooC = new ClassC
                {
                    FooA = new ClassA
                    {
                    }
                }
            });

            // The recursion in the map of ClassA should not affect the map of ClassB if ClassB has no recursion
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.FooC);
            Assert.IsNotNull(result.FooC.FooA);

            var resultOther = mapper.Map <ClassA, ClassA>(new ClassA
            {
                FooB = new ClassB
                {
                    FooC = new ClassC
                    {
                        FooA = new ClassA
                        {
                        }
                    }
                }
            });

            // ClassA should still map the FooA property as null, otherwise it would mean a stackoverflow
            Assert.IsNotNull(resultOther);
            Assert.IsNotNull(resultOther.FooB);
            Assert.IsNotNull(resultOther.FooB.FooC);
            Assert.IsNull(resultOther.FooB.FooC.FooA);
        }
Ejemplo n.º 12
0
        public void CreateMapIsThreadSafe()
        {
            var mapper = new MemberMapper();

            var thread1 = new Thread(() =>
            {
                while (true)
                {
                    var map = mapper.CreateMap <Source, Destination>();

                    var result = map.MappingFunction(new Source {
                        Foo = 1
                    }, new Destination());

                    Assert.AreEqual(1, result.Foo);

                    mapper.ClearMapCache();
                }
            });

            var thread2 = new Thread(() =>
            {
                while (true)
                {
                    var map = mapper.CreateMap <Source, Destination>();

                    var result = map.MappingFunction(new Source {
                        Foo = 1
                    }, new Destination());

                    Assert.AreEqual(1, result.Foo);

                    mapper.ClearMapCache();
                }
            });

            thread1.Start();
            thread2.Start();

            Thread.Sleep(2000);

            thread1.Abort();
            thread2.Abort();
        }
Ejemplo n.º 13
0
        public void TypeMappingsAreNotCachedWhenRecursionDepthIsLimited()
        {
            var mapper = new MemberMapper();

              mapper.Options.Cloning.MaxCloneDepth = 2;

              mapper.CreateMap<ClassA, ClassA>();

              mapper.CreateMap<ClassB, ClassB>();

              var result = mapper.Map<ClassB, ClassB>(new ClassB
              {
            FooC = new ClassC
            {
              FooA = new ClassA
              {

              }
            }
              });

              Assert.IsNotNull(result);
              Assert.IsNotNull(result.FooC);
              Assert.IsNotNull(result.FooC.FooA);

              var resultOther = mapper.Map<ClassA, ClassA>(new ClassA
              {
            FooB = new ClassB
            {
              FooC = new ClassC
              {
            FooA = new ClassA
            {

            }
              }
            }
              });

              Assert.IsNotNull(resultOther);
              Assert.IsNotNull(resultOther.FooB);
              Assert.IsNotNull(resultOther.FooB.FooC);
              Assert.IsNull(resultOther.FooB.FooC.FooA);
        }
Ejemplo n.º 14
0
        public void TryGetMapForMapWithParameterThrowsForMapWithoutParameter()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType>();

            MemberMap <SourceType, DestinationType, int> map;

            Assert.IsFalse(mapper.TryGetMap <SourceType, DestinationType, int>(out map));
        }
Ejemplo n.º 15
0
        public void GetMapThrowsForMapWithParameter()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType, int>((s, i) => new DestinationType
            {
            });

            mapper.GetMap <SourceType, DestinationType>();
        }
Ejemplo n.º 16
0
        public void HasMapWorksWithParameter()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType, int>((s, i) => new DestinationType
            {
            });

            Assert.IsTrue(mapper.HasMap <SourceType, DestinationType>());
        }
        public void MappingFunctionIsSetCorrectly()
        {
            var mapper = new MemberMapper();

              var map = mapper.CreateMap(typeof(Source), typeof(Destination));

              Assert.IsNotNull(map.MappingFunction);
              Assert.IsNotNull(map as MemberMap<Source, Destination>);
              Assert.IsNotNull(((MemberMap<Source, Destination>)map).MappingFunction);
        }
Ejemplo n.º 18
0
        public void ConversionFunctionIsValidatedOnArgumentCount()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<Source, Destination>(options: (ctx, options) =>
              {
            Expression<Func<string, string, string, int>> func = ((s, y, z) => s.Length);
            options.Convert(func);
              });
        }
Ejemplo n.º 19
0
        public void ConversionFunctionIsValidatedOnArgumentCount()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <Source, Destination>(options: (ctx, options) =>
            {
                Expression <Func <string, string, string, int> > func = ((s, y, z) => s.Length);
                options.Convert(func);
            });
        }
        public void MappingFunctionIsSetCorrectly()
        {
            var mapper = new MemberMapper();

            var map = mapper.CreateMap(typeof(Source), typeof(Destination));

            Assert.IsNotNull(map.MappingFunction);
            Assert.IsNotNull(map as MemberMap <Source, Destination>);
            Assert.IsNotNull(((MemberMap <Source, Destination>)map).MappingFunction);
        }
Ejemplo n.º 21
0
        public void NonPublicInterfaceAccessIsDetected_1()
        {
            var mapper = new MemberMapper();

            mapper.Options.Debug.DebugInformationEnabled = true;

            mapper.CreateMap <ISource, Dest>(src => new Dest
            {
                Valid = !src.Valid
            });

            var map = mapper.CreateMap <Source, Dest>();

            var processor = new MapProposalProcessor(mapper);

            processor.Process(map.DebugInformation.MappingExpression);

            Assert.IsTrue(processor.NonPublicMembersAccessed);
        }
Ejemplo n.º 22
0
        public void TypeMappingsAreNotCachedWhenRecursionDepthIsLimited()
        {
            var mapper = new MemberMapper();

            mapper.Options.Cloning.MaxCloneDepth = 2;

            mapper.CreateMap <ClassA, ClassA>();

            mapper.CreateMap <ClassB, ClassB>();

            var result = mapper.Map <ClassB, ClassB>(new ClassB
            {
                FooC = new ClassC
                {
                    FooA = new ClassA
                    {
                    }
                }
            });

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.FooC);
            Assert.IsNotNull(result.FooC.FooA);

            var resultOther = mapper.Map <ClassA, ClassA>(new ClassA
            {
                FooB = new ClassB
                {
                    FooC = new ClassC
                    {
                        FooA = new ClassA
                        {
                        }
                    }
                }
            });

            Assert.IsNotNull(resultOther);
            Assert.IsNotNull(resultOther.FooB);
            Assert.IsNotNull(resultOther.FooB.FooC);
            Assert.IsNull(resultOther.FooB.FooC.FooA);
        }
Ejemplo n.º 23
0
        public ActionResult Edit(int id)
        {
            var memberMapper = new MemberMapper();
              memberMapper.CreateMap<Semester, SemesterModel>();

              var ctx = new CourseContext();

              var result = memberMapper.Map<Semester, SemesterModel>(ctx.Semesters.FirstOrDefault(c => c.ID == id));

              return View(result);
        }
Ejemplo n.º 24
0
        public void HasMapWorksWithParameter()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType, int>((s, i) => new DestinationType
              {

              });

              Assert.IsTrue(mapper.HasMap<SourceType, DestinationType>());
        }
Ejemplo n.º 25
0
        public void GetMapThrowsForMapWithParameter()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType, int>((s, i) => new DestinationType
              {

              });

              mapper.GetMap<SourceType, DestinationType>();
        }
Ejemplo n.º 26
0
        public void ConversionFunctionIsValidatedOnReturnType()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <Source, Destination>(options: (ctx, options) =>
            {
                //new Expression<Func<int, int>>();
                Expression <Func <int, string> > func = s => s + "X";
                options.Convert(func);
            });
        }
Ejemplo n.º 27
0
        //
        // GET: /Group/Delete/5
        public ActionResult Delete(int id)
        {
            var memberMapper = new MemberMapper();
              memberMapper.CreateMap<Group, GroupModel>();

              var ctx = new CourseContext();

              var result = memberMapper.Map<Group, GroupModel>(ctx.Groups.FirstOrDefault(c => c.ID == id));

              return View(result);
        }
Ejemplo n.º 28
0
        public void CustomMappingsAreReused()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <NestedSourceType, NestedDestinationType>(customMapping: src => new NestedDestinationType
            {
                Testing = src.Test + " " + src.ID
            });

            mapper.CreateMap <SourceType, DestinationType>();

            var source = new SourceType {
                Nested = new NestedSourceType {
                    Test = "test", ID = 10
                }
            };

            var result = mapper.Map <SourceType, DestinationType>(source);

            Assert.AreEqual("test 10", result.Nested.Testing);
        }
Ejemplo n.º 29
0
        public void CustomConventionsCannotUseMembersOnDifferentType()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <CustomConventionSource, CustomConventionDestination>(options: (ctx, option) =>
            {
                if (ctx.Source != null)
                {
                    option.MapProperty(typeof(string).GetProperties().First(), ctx.Destination);
                }
            });
        }
Ejemplo n.º 30
0
        public void DebugInformationIsIncluded()
        {
            var mapper = new MemberMapper();
              mapper.Options.Debug.DebugInformationEnabled = true;

              mapper.CreateMap<SourceType, DestType>();

              var map = mapper.GetMap<SourceType, DestType>();

              Assert.IsNotNull(map.DebugInformation);
              Assert.IsNotNull(map.DebugInformation.MappingExpression);
        }
Ejemplo n.º 31
0
        public ActionResult Create(SemesterModel model)
        {
            var memberMapper = new MemberMapper();
              memberMapper.CreateMap<SemesterModel, Semester>();

              var ctx = new CourseContext();

              ctx.Semesters.Add(memberMapper.Map<SemesterModel, Semester>(model));
              ctx.SaveChanges();

              return RedirectToAction("Index");
        }
Ejemplo n.º 32
0
        public void CreateMapIsThreadSafe()
        {
            var mapper = new MemberMapper();

              var thread1 = new Thread(() =>
              {
            while (true)
            {
              var map = mapper.CreateMap<Source, Destination>();

              var result = map.MappingFunction(new Source { Foo = 1 }, new Destination());

              Assert.AreEqual(1, result.Foo);

              mapper.ClearMapCache();
            }
              });

              var thread2 = new Thread(() =>
              {
            while (true)
            {
              var map = mapper.CreateMap<Source, Destination>();

              var result = map.MappingFunction(new Source { Foo = 1 }, new Destination());

              Assert.AreEqual(1, result.Foo);

              mapper.ClearMapCache();
            }
              });

              thread1.Start();
              thread2.Start();

              Thread.Sleep(2000);

              thread1.Abort();
              thread2.Abort();
        }
Ejemplo n.º 33
0
        public void ComplexMappingTakesPrecedenceOverNormalMapping()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SimpleSourceType, SimpleDestinationType>(customMapping: src => new SimpleDestinationType
              {
            Foo = "Foo"
              });

              var result = mapper.Map<SimpleSourceType, SimpleDestinationType>(new SimpleSourceType { Foo = "Bar" });

              Assert.AreEqual("Foo", result.Foo);
        }
Ejemplo n.º 34
0
        public void ConversionFunctionBetweenTypesWorks()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<IntType, StringType>(options: (ctx, options) =>
              {
            options.Convert<int, string>(i => i.ToString() + "test");
              });

              var result = mapper.Map<IntType, StringType>(new IntType { Foo = 10 });

              Assert.AreEqual("10test", result.Foo);
        }
Ejemplo n.º 35
0
        public void SourceMapperOptionsInContextAreRespected_WithInheritence()
        {
            var mapper = new MemberMapper();

            var options = new MapperOptions();

            options.Debug.DebugInformationEnabled = true;

            mapper.ForSourceType <Source>().UseMapperOptions(options);

            var map = mapper.CreateMap <SourceInherited, DestinationInherited>();

            Assert.IsNotNull(map.DebugInformation.MappingExpression);

            mapper.ClearMapCache();

            options.Debug.DebugInformationEnabled = false;

            map = mapper.CreateMap <SourceInherited, DestinationInherited>();

            Assert.IsNull(map.DebugInformation);
        }
Ejemplo n.º 36
0
        public void ComplexMappingIsUsedWhenDestinationPropertyIsString()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<ComplexSourceType, SimpleDestinationType>(customMapping: src => new SimpleDestinationType
              {
             Foo = src.Foo.Name
              });

              var result = mapper.Map<ComplexSourceType, SimpleDestinationType>(new ComplexSourceType { Foo = new ComplexSourceTypeNested { Name = "Test" } });

              Assert.AreEqual("Test", result.Foo);
        }
Ejemplo n.º 37
0
        public void DebugInformationIsIncluded()
        {
            var mapper = new MemberMapper();

            mapper.Options.Debug.DebugInformationEnabled = true;

            mapper.CreateMap <SourceType, DestType>();

            var map = mapper.GetMap <SourceType, DestType>();

            Assert.IsNotNull(map.DebugInformation);
            Assert.IsNotNull(map.DebugInformation.MappingExpression);
        }
Ejemplo n.º 38
0
        public void ConversionToUtcTimeWithCustomMappingWorks()
        {
            var mapper = new MemberMapper();

            MemberOptions func = new MemberOptions((ctx, options) =>
            {
                if (ctx.Source.DeclaringType == typeof(SourceDate))
                {
                    options.Convert <DateTime, DateTime>(d => d.ToUniversalTime());
                }
                else
                {
                    options.Convert <DateTime, DateTime>(d => d.ToLocalTime());
                }
            });

            mapper.CreateMap <SourceDate, DestinationDate>(options: func,
                                                           customMapping: src => new DestinationDate
            {
                Start = new DateTime(2001, 12, 1, 10, 0, 0, DateTimeKind.Local)
            });

            mapper.CreateMap <DestinationDate, SourceDate>(options: func);

            var result = mapper.Map(new SourceDate {
                Start = DateTime.Now
            }, new DestinationDate());

            Assert.AreEqual(2001, result.Start.Year);
            Assert.AreEqual(DateTimeKind.Utc, result.Start.Kind);

            var reverse = mapper.Map(new DestinationDate {
                Start = DateTime.Now
            }, new SourceDate());

            Assert.AreEqual(DateTimeKind.Local, reverse.Start.Kind);
        }
Ejemplo n.º 39
0
        public ActionResult Details(int id)
        {
            var memberMapper = new MemberMapper();
              memberMapper.CreateMap<Teacher, TeacherModel>(source => new TeacherModel()
              {
            RoomNr = source.RoomNumber,
            SchoolPhone = source.Phone,
            Identifier = source.IDNumber
              });

              var ctx = new CourseContext();

              var result = memberMapper.Map<Teacher, TeacherModel>(ctx.Teachers.FirstOrDefault(c => c.ID == id));
              return View(result);
        }
Ejemplo n.º 40
0
        public void ComplexMappingTakesPrecedenceOverNormalMapping()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SimpleSourceType, SimpleDestinationType>(customMapping: src => new SimpleDestinationType
            {
                Foo = "Foo"
            });

            var result = mapper.Map <SimpleSourceType, SimpleDestinationType>(new SimpleSourceType {
                Foo = "Bar"
            });

            Assert.AreEqual("Foo", result.Foo);
        }
Ejemplo n.º 41
0
        public void ConversionFunctionWorks()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <Source, Destination>(options: (ctx, options) =>
            {
                options.Convert <int, int>(s => s * 2);
            });

            var result = mapper.Map(new Source {
                Foo = 2
            }, new Destination());

            Assert.AreEqual(4, result.Foo);
        }
Ejemplo n.º 42
0
        public void MappingDefinedOnSourceBaseTypeOnlyIsRespectedForIDestFromSource()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <Source, IDest>(src => new Dest
            {
                Bar = src.Foo * 10
            });

            var result = mapper.Map <Source, Dest>(new Source {
                Foo = 10
            });

            Assert.AreEqual(100, result.Bar);
        }
        public void CustomMappingsWorkWithProjections()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType>(src => new DestinationType
              {
            FullName = src.FirstName + " " + src.LastName
              });

              var projection = mapper.Project<SourceType, DestinationType>().Compile();

              var result = projection(new SourceType { FirstName = "First", LastName = "Last" });

              Assert.AreEqual("First Last", result.FullName);
        }
Ejemplo n.º 44
0
        public void ConversionFunctionWorksForSubtypes()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <IntType, StringType>(options: (ctx, options) =>
            {
                options.Convert <int, string>(i => i.ToString() + "test");
            });

            var result = mapper.Map <IntTypeInherited, StringTypeInherited>(new IntTypeInherited {
                Foo = 10
            });

            Assert.AreEqual("10test", result.Foo);
        }
Ejemplo n.º 45
0
        public void ParameterIsUsed()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType, int>((src, i) => new DestinationType
            {
                ID = i
            });

            var result = mapper.Map(new SourceType(), new DestinationType(), 10);

            Assert.AreEqual(10, result.ID);

            result = mapper.Map(new SourceType(), new DestinationType(), 15);

            Assert.AreEqual(15, result.ID);
        }
Ejemplo n.º 46
0
        public void ComplexMappingIsUsedWhenDestinationPropertyIsString()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <ComplexSourceType, SimpleDestinationType>(customMapping: src => new SimpleDestinationType
            {
                Foo = src.Foo.Name
            });

            var result = mapper.Map <ComplexSourceType, SimpleDestinationType>(new ComplexSourceType {
                Foo = new ComplexSourceTypeNested {
                    Name = "Test"
                }
            });

            Assert.AreEqual("Test", result.Foo);
        }
Ejemplo n.º 47
0
        public ActionResult Add(TeacherModel model)
        {
            var memberMapper = new MemberMapper();
              memberMapper.CreateMap<TeacherModel, Teacher>(source =>
            new Teacher()
            {
              Phone = source.SchoolPhone,
              IDNumber = source.Identifier,
              RoomNumber = source.RoomNr
            });

              CourseContext ctx = new CourseContext();
              ctx.Teachers.Add(memberMapper.Map<Teacher>(model));
              ctx.SaveChanges();

              return RedirectToAction("Index");
        }
Ejemplo n.º 48
0
        public void ParameterIsUsed()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType, int>((src, i) => new DestinationType
              {
            ID = i
              });

              var result = mapper.Map(new SourceType(), new DestinationType(), 10);

              Assert.AreEqual(10, result.ID);

              result = mapper.Map(new SourceType(), new DestinationType(), 15);

              Assert.AreEqual(15, result.ID);
        }
        public void CustomMappingsWorkWithProjections()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceType, DestinationType>(src => new DestinationType
            {
                FullName = src.FirstName + " " + src.LastName
            });

            var projection = mapper.Project <SourceType, DestinationType>().Compile();

            var result = projection(new SourceType {
                FirstName = "First", LastName = "Last"
            });

            Assert.AreEqual("First Last", result.FullName);
        }
Ejemplo n.º 50
0
        public static void CompileTest()
        {
            var sw = Stopwatch.StartNew();

            var mapper = new MemberMapper();

            for (var i = 0; i < 1000; i++)
            {
                mapper.CreateMap <Customer, CustomerDto>(customMapping: src => new CustomerDto
                {
                    FullName    = src.FirstName + " " + src.LastName,
                    OrderAmount = src.Orders.Sum(o => o.Amount)
                });
            }

            Console.WriteLine(sw.Elapsed);
        }
Ejemplo n.º 51
0
        public void BaseClassMappingIsRespected()
        {
            var mapper = new MemberMapper();

            mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;

            mapper.CreateMap <SourceBase, DestinationBase>(customMapping: src => new DestinationBase
            {
                Test = src.ID
            });

            var result = mapper.Map <SourceInherited, DestinationInherited>(new SourceInherited {
                ID = 10
            });

            Assert.AreEqual(10, result.Test);
        }
Ejemplo n.º 52
0
        public void MappingDefinedOnInterfaceIsRespected()
        {
            var mapper = new MemberMapper();

            mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;

            mapper.CreateMap <ImplementationSource, Interface>(customMapping: src => new
            {
                Foo = src.Bar
            });


            var result = mapper.Map <ImplementationSource, Implementation>(new ImplementationSource {
                Bar = "test"
            });

            Assert.AreEqual("test", result.Foo);
        }
Ejemplo n.º 53
0
        public void DestinationMapperOptionsTakePriorityOverSourceMapperOption()
        {
            var mapper = new MemberMapper();

            var options = new MapperOptions();

            options.Debug.DebugInformationEnabled = true;

            var options1 = new MapperOptions();

            options1.Debug.DebugInformationEnabled = false;

            mapper.ForDestinationType <Destination>().UseMapperOptions(options);
            mapper.ForSourceType <Source>().UseMapperOptions(options1);

            var map = mapper.CreateMap <Source, Destination>();

            Assert.IsNotNull(map.DebugInformation.MappingExpression);
        }
Ejemplo n.º 54
0
        public void ConversionFunctionAndCustomMappingWorks()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<Source, Destination>(

              customMapping: src => new Destination
              {
            Foo = src.Foo * 3
              },

              options: (ctx, options) =>
              {
            options.Convert<int, int>(s => s * 2);
              });

              var result = mapper.Map(new Source { Foo = 2 }, new Destination());

              Assert.AreEqual(12, result.Foo);
        }
Ejemplo n.º 55
0
        public void NewExpressionCanBeUsedAsMapping()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <SourceConstructor, DestinationConstructor>(src => new DestinationConstructor
            {
                Nested = new DestinationConstructorNested
                {
                    Foobar = src.Foo + "x",
                    Test   = src.Foo + src.Foo
                }
            });

            var result = mapper.Map(new SourceConstructor
            {
                Foo = "12"
            }, new DestinationConstructor());

            Assert.AreEqual("12x", result.Nested.Foobar);
            Assert.AreEqual("1212", result.Nested.Test);
        }
Ejemplo n.º 56
0
        public void ConversionIsCarriedOverToSubtypes()
        {
            var mapper = new MemberMapper();

            MemberOptions func = new MemberOptions((ctx, options) =>
            {
                if (typeof(IDataModel).IsAssignableFrom(ctx.Source.DeclaringType) && typeof(DtoBase).IsAssignableFrom(ctx.Destination.DeclaringType))
                {
                    options.Convert <DateTime, DateTime>(d => d.ToUniversalTime());
                }
            });

            mapper.CreateMap <Customer, CustomerDto>(options: func);

            var result = mapper.Map <Customer, CustomerDto>(new Customer
            {
                CreationTime = DateTime.Now
            });

            Assert.AreEqual(DateTimeKind.Utc, result.CreationTime.Kind);
        }
Ejemplo n.º 57
0
        public void ConversionFunctionAndCustomMappingWorks()
        {
            var mapper = new MemberMapper();

            mapper.CreateMap <Source, Destination>(

                customMapping: src => new Destination
            {
                Foo = src.Foo * 3
            },

                options: (ctx, options) =>
            {
                options.Convert <int, int>(s => s * 2);
            });

            var result = mapper.Map(new Source {
                Foo = 2
            }, new Destination());

            Assert.AreEqual(12, result.Foo);
        }
Ejemplo n.º 58
0
        public void UsingNonGenericMapAsGenericWorks()
        {
            var mapper = new MemberMapper();
              mapper.CreateMap(typeof(SourceType), typeof(DestinationType));

              var result = mapper.Map<SourceType, DestinationType>(new SourceType());
        }
Ejemplo n.º 59
0
        public void TryGetMapThrowsForMapWithParameter()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap<SourceType, DestinationType, int>((s, i) => new DestinationType
              {

              });
              MemberMap<SourceType, DestinationType> map;

              Assert.IsFalse(mapper.TryGetMap<SourceType, DestinationType>(out map));
        }
Ejemplo n.º 60
0
        public void NonGenericMapForMapWithParameterThrowsForMapWithoutParameter()
        {
            var mapper = new MemberMapper();

              mapper.CreateMap(typeof(SourceType), typeof(DestinationType));

              mapper.Map<SourceType, DestinationType, int>(new SourceType(), 0);
        }