public static KeyValuePair<TestModel, TestViewModel> AccessSourceNestedProperty()
        {
            var testId = Guid.NewGuid();

            var created = DateTime.UtcNow;
            var src = new TestModel
            {
                Id = testId,
                Age = 18,
                Created = created,
                Name = "AutoMemberMap",
                Weight = 32
            };

            var result = new TestViewModel
            {
                Id = testId,
                Age = 18,
                Created = created,
                Weight = 32
            };

            return new KeyValuePair<TestModel, TestViewModel>(src, result);
        }
        public void EnumMap()
        {
            Mapper.Register<TestModel, TestViewModel>()
                .Ignore(x => x.Country)
                .Ignore(x => x.Sizes)
                .Member(x => x.GenderIndex, x => x.NullableGender);
            Mapper.Compile();

            var test = new TestModel()
            {
                Gender = GenderTypes.Men.ToString(),
                NullableGender = GenderTypes.Women
            };

            var testViewModel = Mapper.Map<TestModel, TestViewModel>(test);

            Assert.AreEqual(GenderTypes.Men, testViewModel.Gender);
            Assert.AreEqual(GenderTypes.Women.ToString(), testViewModel.NullableGender);
            Assert.AreEqual((int)GenderTypes.Women, testViewModel.GenderIndex);
        }
        public void ConvertibleMap()
        {
            Mapper.Register<TestModel, TestViewModel>()
                .Ignore(x => x.Country)
                .Ignore(x => x.Sizes)
                .Member(x => x.GenderIndex, x => x.BoolValue)
                .Member(x => x.NotNullable, x => x.Height);
            Mapper.Compile();

            var test = new TestModel()
            {
                BoolValue = true,
                Height = 123
            };

            var testViewModel = Mapper.Map<TestModel, TestViewModel>(test);

            Assert.AreEqual(1, testViewModel.GenderIndex);
            Assert.AreEqual(123, testViewModel.NotNullable);
        }
        public static KeyValuePair<TestModel, TestViewModel> CustomNestedCollectionMap()
        {
            var newGuid = Guid.NewGuid();
            const string xxxl = "XXXL";
            const string xxxlSize = "XXXL Size";
            const int sortOrder = 2;

            var testId = Guid.NewGuid();
            var countryId = Guid.NewGuid();

            var created = DateTime.UtcNow;
            var name = string.Format("USA-1");
            var code = "US";
            var automembermap = "AutoMemberMap";
            var age = 43;
            var src = new TestModel
            {
                Id = testId,
                Age = age,
                Country = new Country
                {
                    Id = countryId,
                    Name = name,
                    Code = code
                },
                Created = created,
                Name = automembermap,
                Weight = 23.6M,
                Sizes = new List<Size>
                {
                    new Size
                {
                    Id = newGuid,
                    Alias = xxxl,
                    Name = xxxlSize,
                    SortOrder = sortOrder
                }
                }
            };

            var rst = new TestViewModel
            {
                Id = testId,
                Age = age,
                Country = new CountryViewModel
                {
                    Id = countryId,
                    Name = name,
                    Code = code
                },
                Created = created,
                Name = automembermap,
                Weight = 23.6M,
                Sizes = new List<SizeViewModel>
                {
                    new SizeViewModel
                {
                    Id = newGuid,
                    Alias = xxxl,
                    Name = xxxlSize,
                    SortOrder = sortOrder
                }
                }
            };

            var keyValuePair = new KeyValuePair<TestModel, TestViewModel>
                (
                src,
                rst
                );
            return keyValuePair;
        }
        public static KeyValuePair<List<TestModel>, List<TestViewModel>> CollectionAutoMemberMap()
        {
            var source = new List<TestModel>();
            var result = new List<TestViewModel>();
            Func<string, int, string> iterateFormatFunc = (str, ind) => string.Format("{0}-{1}", str, ind);

            for (var i = 0; i < 10; i++)
            {
                var testId = Guid.NewGuid();
                var countryId = Guid.NewGuid();

                var created = DateTime.UtcNow;
                var name = string.Format("USA-{0}", i);
                var code = iterateFormatFunc("US", i);
                var automembermap = iterateFormatFunc("AutoMemberMap", i);
                var age = i;
                var src = new TestModel
                {
                    Id = testId,
                    Age = age,
                    Country = new Country
                    {
                        Id = countryId,
                        Name = name,
                        Code = code
                    },
                    Created = created,
                    Name = automembermap,
                    Weight = i % 2
                };

                var rst = new TestViewModel
                {
                    Id = testId,
                    Age = age,
                    Country = new CountryViewModel
                    {
                        Id = countryId,
                        Name = name,
                        Code = code
                    },
                    Created = created,
                    Name = automembermap,
                    Weight = i % 2
                };

                source.Add(src);
                result.Add(rst);
            }

            return new KeyValuePair<List<TestModel>, List<TestViewModel>>(source, result);
        }
        public static KeyValuePair<TestModel, TestViewModel> NullPropertyAndNullCollectionMap()
        {
            var newGuid = Guid.NewGuid();
            const string xxxl = "XXXL";
            const string xxxlSize = "XXXL Size";
            const int sortOrder = 2;

            var testId = Guid.NewGuid();
            var countryId = Guid.NewGuid();

            var created = DateTime.UtcNow;
            var name = string.Format("USA-1");
            var code = "US";
            var automembermap = "AutoMemberMap";
            var age = 43;
            var src = new TestModel
            {
                Id = testId,
                Age = age,
                Created = created,
                Name = automembermap,
                Weight = 23.6M,
            };

            var rst = new TestViewModel
            {
                Id = testId,
                Age = age,
                Created = created,
                Name = automembermap,
                Weight = 23.6M,
            };

            var keyValuePair = new KeyValuePair<TestModel, TestViewModel>
                (
                src,
                rst
                );
            return keyValuePair;
        }
        public static KeyValuePair<TestModel, TestViewModel> ExistingDestinationSimpleMap()
        {
            var testId = Guid.NewGuid();
            var countryId = Guid.NewGuid();

            var created = DateTime.UtcNow;
            var xxlId = Guid.NewGuid();
            var xlId = Guid.NewGuid();
            var src = new TestModel
            {
                Id = testId,
                Age = 18,
                Country = new Country
                {
                    Id = countryId,
                    Name = "USA - changed",
                    Code = "US - changed"
                },
                Created = created,
                Name = "AutoMemberMap - changed",
                Sizes = new List<Size>
                {
                    new Size
                    {
                        Id = xxlId,
                        Name = "XXL size - changed",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new Size
                    {
                        Id = xlId,
                        Name = "XL size - CHANGED",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                Weight = 32
            };

            var result = new TestViewModel
            {
                Id = testId,
                Age = 18,
                Country = new CountryViewModel
                {
                    Id = countryId,
                    Name = "USA",
                    Code = "US"
                },
                Created = created,
                Name = "AutoMemberMap",
                Sizes = new List<SizeViewModel>
                {
                    new SizeViewModel
                    {
                        Id = xxlId,
                        Name = "XXL size",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new SizeViewModel
                    {
                        Id = xlId,
                        Name = "XL size",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                Weight = 32
            };

            return new KeyValuePair<TestModel, TestViewModel>(src, result);
        }
        public static KeyValuePair<TestModel, TestViewModel> AutoMemberMap()
        {
            var testId = Guid.NewGuid();
            var countryId = Guid.NewGuid();

            var created = DateTime.UtcNow;
            var xxlId = Guid.NewGuid();
            var xlId = Guid.NewGuid();
            var src = new TestModel
            {
                Id = testId,
                Age = 18,
                Country = new Country
                {
                    Id = countryId,
                    Name = "USA",
                    Code = "US"
                },
                Created = created,
                Name = "AutoMemberMap",
                Sizes = new List<Size>
                {
                    new Size
                    {
                        Id = xxlId,
                        Name = "XXL size",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new Size
                    {
                        Id = xlId,
                        Name = "XL size",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                Weight = 32,
                CaseInsensitive = "abc"
            };

            var result = new TestViewModel
            {
                Id = testId,
                Age = 18,
                Country = new CountryViewModel
                {
                    Id = countryId,
                    Name = "USA",
                    Code = "US"
                },
                Created = created,
                Name = "AutoMemberMap",
                Sizes = new List<SizeViewModel>
                {
                    new SizeViewModel
                    {
                        Id = xxlId,
                        Name = "XXL size",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new SizeViewModel
                    {
                        Id = xlId,
                        Name = "XL size",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                Weight = 32,
                CaSeInSeNsItIvE = "abc"
            };

            return new KeyValuePair<TestModel, TestViewModel>(src, result);
        }
Exemple #9
0
        public static KeyValuePair<List<TestModel>, List<TestViewModel>> AutoMemberMapCollection()
        {
            var models = new List<TestModel>();
            var viewModels = new List<TestViewModel>();
            for (var i = 0; i < 10; i++)
            {
                var testId = Guid.NewGuid();
                var countryId = Guid.NewGuid();

                var created = DateTime.UtcNow;
                var xxlId = Guid.NewGuid();
                var xlId = Guid.NewGuid();
                var src = new TestModel
                {
                    Id = testId,
                    Age = 18,
                    Country = new Country
                    {
                        Id = countryId,
                        Name = "USA",
                        Code = "US"
                    },
                    Created = created,
                    Name = "AutoMemberMap",
                    Sizes = new List<Size>
                {
                    new Size
                    {
                        Id = xxlId,
                        Name = "XXL size",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new Size
                    {
                        Id = xlId,
                        Name = "XL size",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                    Weight = 32,
                    CaseInsensitive = "abc"
                };
                models.Add(src);

                var result = new TestViewModel
                {
                    Id = testId,
                    Age = 18,
                    Country = new CountryViewModel
                    {
                        Id = countryId,
                        Name = "USA",
                        Code = "US"
                    },
                    Created = created,
                    Name = "AutoMemberMap",
                    Sizes = new List<SizeViewModel>
                {
                    new SizeViewModel
                    {
                        Id = xxlId,
                        Name = "XXL size",
                        Alias = "XXL",
                        SortOrder = 3
                    },
                    new SizeViewModel
                    {
                        Id = xlId,
                        Name = "XL size",
                        Alias = "XL",
                        SortOrder = 2
                    }
                },
                    Weight = 32,
                    CaSeInSeNsItIvE = "abc"
                };
                viewModels.Add(result);
            }

            return new KeyValuePair<List<TestModel>, List<TestViewModel>>(models, viewModels);
        }