Example #1
0
        public void IgnoreProperty()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <Person, PersonReportModel>(p => new PersonReportModel
            {
                Name    = $"{p.FirstName} {p.LastName}",
                Age     = TestHelpers.CalculateAge(p.BirthDate),
                Address = MappingOptions.Ignore()
            });

            var reportModel = mapperConfiguration.Convert <Person, PersonReportModel>(TestData.People.JohnSmith);

            Assert.IsNull(reportModel.Address);
            const string doNotChange   = "DO NOT CHANGE THIS";
            var          existingModel = new PersonReportModel
            {
                Name        = "Test",
                Address     = doNotChange,
                City        = "Test",
                PhoneNumber = "Test",
                Age         = 5
            };

            mapperConfiguration.MapData(TestData.People.JohnSmith, existingModel);
            Assert.AreEqual(doNotChange, existingModel.Address);
        }
        public void ChangeDestinationBeforeMapTest()
        {
            var mappingConfiguration = new MappingConfiguration();

            mappingConfiguration.CreateMapping <PersonFrontModel, Person>(
                f => new Person
            {
                PersonId  = MappingOptions.Ignore(),
                Timestamp = MappingOptions.Ignore(),
                FirstName = MappingOptions.Ignore()
            },
                s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                .DestinationNamingConvention(NamingConventionType.CamelCase)
                .BeforeMap((f, p) =>
            {
                if (p.PersonId == 0)
                {
                    p.PersonId = 10;
                }
                p.FirstName = "Brent";
            }));
            var frontModel = new PersonFrontModel();
            var newPerson  = mappingConfiguration.Convert(frontModel).To <Person>();

            Assert.AreEqual(10, newPerson.PersonId);
            Assert.AreEqual("Brent", newPerson.FirstName);
            var existingPerson = new Person {
                PersonId = 9
            };

            mappingConfiguration.MapData(frontModel, existingPerson);
            Assert.AreEqual(9, existingPerson.PersonId);
            Assert.AreEqual("Brent", existingPerson.FirstName);
        }
Example #3
0
        public void MapModelWithCtor()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <Person, PersonWithCtor>(p => new PersonWithCtor(p.FirstName, p.LastName, p.PersonId)
            {
                StreetAddress = p.Address,
            });

            var modelWithCtor = mapperConfiguration.Convert <Person, PersonWithCtor>(TestData.People.JohnSmith);

            CheckPersonWithCtor(TestData.People.JohnSmith, modelWithCtor);

            var existingModelWithCtor = new PersonWithCtor(TestData.People.JohnSmith.FirstName,
                                                           TestData.People.JohnSmith.LastName, TestData.People.JohnSmith.PersonId);

            mapperConfiguration.MapData(TestData.People.JohnSmith, existingModelWithCtor);
            CheckPersonWithCtor(TestData.People.JohnSmith, existingModelWithCtor);
        }
Example #4
0
        public void MapSimpleModel()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <Person, PersonReportModel>(p => new PersonReportModel
            {
                Name = $"{p.FirstName} {p.LastName}",
                Age  = TestHelpers.CalculateAge(p.BirthDate),
            });

            var reportModel = mapperConfiguration.Convert <Person, PersonReportModel>(TestData.People.JohnSmith);

            CheckPersonReportModel(TestData.People.JohnSmith, reportModel);

            var existingReportModel = new PersonReportModel();

            mapperConfiguration.MapData(TestData.People.JohnSmith, existingReportModel);
            CheckPersonReportModel(TestData.People.JohnSmith, existingReportModel);
        }
Example #5
0
        public void IsConstructionFlag()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <PersonFrontModel, Person>(
                f => new Person
            {
                Height    = MappingOptions.IsConstruction() ? 6 : 7,
                Timestamp = MappingOptions.Ignore()
            },
                s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                .DestinationNamingConvention(NamingConventionType.CamelCase));
            var frontModel      = new PersonFrontModel();
            var convertedPerson = mapperConfiguration.Convert(frontModel).To <Person>();
            var existingPerson  = new Person();

            mapperConfiguration.MapData(frontModel, existingPerson);
            Assert.AreEqual(6, convertedPerson.Height);
            Assert.AreEqual(7, existingPerson.Height);
        }
        public void PrefixesTest()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <Person, PersonWeirdModel>(p => new PersonWeirdModel(),
                                                                         s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                                                                         .DestinationNamingConvention(NamingConventionType.UnderscoreSeparated, "s_", "i_", "copy_"));

            var otherModel = mapperConfiguration.Convert <Person, PersonWeirdModel>(TestData.People.JohnSmith);

            CheckWeirdModel(TestData.People.JohnSmith, otherModel);
            mapperConfiguration.CreateMapping <PersonWeirdModel, Person>(p => new Person(),
                                                                         s => s.UnresolvedBehavior(UnresolvedPropertyBehavior.Ignore)
                                                                         .CollisionBehavior(SelectSourceCollisionBehavior.ChooseAny)
                                                                         .SourceNamingConvention(NamingConventionType.UnderscoreSeparated, "s_", "i_", "copy_")
                                                                         .DestinationNamingConvention(NamingConventionType.CamelCase));
            var newPerson = new Person();

            mapperConfiguration.MapData(otherModel, newPerson);
            CheckFromWeirdModel(otherModel, newPerson);
        }
Example #7
0
        public void IgnoreTest()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <User, Person, UserWeirdModel>(
                (u, p) => new UserWeirdModel
            {
                home_address   = p.Address,
                johns_password = p.FirstName == "John" ? u.Password : MappingOptions.Ignore(),
                x_some_field   = MappingOptions.Ignore()
            },
                s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                .DestinationNamingConvention(NamingConventionType.UnderscoreSeparated, "s_", "x_"));
            var johnUser = mapperConfiguration.Convert(TestData.Users.JohnUser, TestData.People.JohnSmith).To <UserWeirdModel>();

            CheckUserWierdModel(TestData.Users.JohnUser, TestData.People.JohnSmith, johnUser);
            var brentUser = johnUser;

            mapperConfiguration.MapData(TestData.Users.BrentUser, TestData.People.BrentJohnson, brentUser);
            CheckUserWierdModel(TestData.Users.BrentUser, TestData.People.BrentJohnson, brentUser);
        }
Example #8
0
        public void ConditionalIgnore()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <Person, PersonReportModel>(p => new PersonReportModel
            {
                Name        = p.FirstName != "Brent" ? "Not Brent" : "Brent",
                Age         = p.FirstName == "Brent" ? 6 : 5,
                Address     = p.FirstName == "John" ? p.Address : MappingOptions.Ignore(),
                City        = p.FirstName == "John" ? MappingOptions.Ignore() : p.City,
                PhoneNumber = p.FirstName != "John" ? MappingOptions.Ignore() : p.PhoneNumber
            });
            mapperConfiguration.CreateMapping <Person, Person>(p => new Person());
            var reportModel = mapperConfiguration.Convert <Person, PersonReportModel>(TestData.People.JohnSmith);

            Assert.AreEqual(TestData.People.JohnSmith.Address, reportModel.Address);
            Assert.IsNull(reportModel.City);
            Assert.AreEqual(TestData.People.JohnSmith.PhoneNumber, reportModel.PhoneNumber);
            Assert.AreEqual("Not Brent", reportModel.Name);
            Assert.AreEqual(5, reportModel.Age);

            const string doNotChange = "DO NOT CHANGE THIS";

            var existingModel = new PersonReportModel
            {
                Name        = "Brent",
                Address     = doNotChange,
                PhoneNumber = doNotChange,
                City        = "Test",
                Age         = 5
            };
            var brent = mapperConfiguration.Convert <Person, Person>(TestData.People.JohnSmith);

            brent.FirstName = "Brent";
            mapperConfiguration.MapData(brent, existingModel);
            Assert.AreEqual(doNotChange, existingModel.Address);
            Assert.AreEqual(doNotChange, existingModel.PhoneNumber);
            Assert.AreEqual(brent.City, existingModel.City);
            Assert.AreEqual(6, existingModel.Age);
        }
Example #9
0
        public void IsConstructionWithIgnoreTest()
        {
            var mapperConfiguration = new MappingConfiguration();

            mapperConfiguration.CreateMapping <PersonFrontModel, Person>(
                f => new Person
            {
                PersonId  = MappingOptions.IsConstruction() ? 10 : MappingOptions.Ignore(),
                Timestamp = MappingOptions.Ignore()
            },
                s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                .DestinationNamingConvention(NamingConventionType.CamelCase));

            var frontModel      = new PersonFrontModel();
            var convertedPerson = mapperConfiguration.Convert(frontModel).To <Person>();
            var existingPerson  = new Person {
                PersonId = 9
            };

            mapperConfiguration.MapData(frontModel, existingPerson);
            Assert.AreEqual(10, convertedPerson.PersonId);
            Assert.AreEqual(9, existingPerson.PersonId);
        }
Example #10
0
        public void AfterMapWithMultipleSources()
        {
            var mappingConfiguration = new MappingConfiguration();

            mappingConfiguration.CreateMapping <PersonFrontModel, int, Person>(
                (f, newId) => new Person
            {
                PersonId  = MappingOptions.Ignore(),
                Timestamp = MappingOptions.Ignore(),
                FirstName = MappingOptions.Ignore(),
                LastName  = "Johnson"
            },
                s => s.SourceNamingConvention(NamingConventionType.CamelCase)
                .DestinationNamingConvention(NamingConventionType.CamelCase)
                .AfterMap((f, newId, p) =>
            {
                if (p.PersonId == 0)
                {
                    p.PersonId = newId;
                }
                p.FirstName = "Brent";
                p.LastName  = "Smith";
            }));
            var frontModel = new PersonFrontModel();
            var newPerson  = mappingConfiguration.Convert(frontModel, 10).To <Person>();

            Assert.AreEqual(10, newPerson.PersonId);
            Assert.AreEqual("Brent", newPerson.FirstName);
            Assert.AreEqual("Smith", newPerson.LastName);
            var existingPerson = new Person {
                PersonId = 9
            };

            mappingConfiguration.MapData(frontModel, 10, existingPerson);
            Assert.AreEqual(9, existingPerson.PersonId);
            Assert.AreEqual("Smith", existingPerson.LastName);
        }