Пример #1
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);
        }
Пример #2
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);
        }
Пример #3
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();
        }
Пример #4
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();
        }
Пример #5
0
        public static void DirectMapping()
        {
            var customer = new Customer
              {
            ID = 1,
            FirstName = "First",
            LastName = "Last",
            Orders = new List<Order>
            {
              new Order
              {
            ID = 1,
            Amount = 10,
              },
              new Order
              {
            ID = 2,
            Amount = 20
              }
            }
              };

              var mapper = new MemberMapper();

              // Explicit source and destination type, just pass a source type instance
              var dto = mapper.Map<Customer, CustomerDto>(customer);

              // Just specify what ThisMember should map to, pass in any type that you think can be mapped
              dto = mapper.Map<CustomerDto>(customer);

              // Update the existing Customer, just set a new FirstName for him
              dto = new CustomerDto
              {
            FirstName = "NewName"
              };

              // For that we have to set an option that null-values from the source are ignored, so that LastName does not get set to null
              mapper.Options.Conventions.IgnoreMembersWithNullValueOnSource = true;

              // Setting an option that affects map generation has no effect on maps that have already been generated. Normally there'd be little
              // need to set this option on the fly, you would just have a seperate mapper for doing these mappings. But for this sample, it works fine.
              mapper.ClearMapCache(); // We could also have called mapper.CreateMap<CustomerDto, Customer>(), which always recreates the map.

              // Only FirstName will have changed now on customer, because the null (or null-equivalent in case of nullable value types) values were ignored.
              mapper.Map(dto, customer);
        }
Пример #6
0
        public static void DirectMapping()
        {
            var customer = new Customer
            {
                ID        = 1,
                FirstName = "First",
                LastName  = "Last",
                Orders    = new List <Order>
                {
                    new Order
                    {
                        ID     = 1,
                        Amount = 10,
                    },
                    new Order
                    {
                        ID     = 2,
                        Amount = 20
                    }
                }
            };

            var mapper = new MemberMapper();

            // Explicit source and destination type, just pass a source type instance
            var dto = mapper.Map <Customer, CustomerDto>(customer);

            // Just specify what ThisMember should map to, pass in any type that you think can be mapped
            dto = mapper.Map <CustomerDto>(customer);

            // Update the existing Customer, just set a new FirstName for him
            dto = new CustomerDto
            {
                FirstName = "NewName"
            };

            // For that we have to set an option that null-values from the source are ignored, so that LastName does not get set to null
            mapper.Options.Conventions.IgnoreMembersWithNullValueOnSource = true;

            // Setting an option that affects map generation has no effect on maps that have already been generated. Normally there'd be little
            // need to set this option on the fly, you would just have a seperate mapper for doing these mappings. But for this sample, it works fine.
            mapper.ClearMapCache(); // We could also have called mapper.CreateMap<CustomerDto, Customer>(), which always recreates the map.

            // Only FirstName will have changed now on customer, because the null (or null-equivalent in case of nullable value types) values were ignored.
            mapper.Map(dto, customer);
        }
Пример #7
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);
        }