简体   繁体   中英

Automapper - Map model from lowercase to pascal case

I have two models.

Source model:

public sealed class adresse
{
        public string strasse { get; set; }
        public string hausnummer { get; set; }
        public string plz { get; set; }
        public string ort { get; set; }
        public string landCode { get; set; }
}

Destination model:

public sealed class Adresse
{
        public string Strasse { get; set; }
        public string Hausnummer { get; set; }
        public string Plz { get; set; }
        public string Ort { get; set; }
        public string LandCode { get; set; }
}

Therefore I created a mapping with automapper and a unit test.

public class AddressMapper
    {
        public Address map()
        {
            adresse add = new adresse();
            add.hausnummer = "1";
            add.ort = "Test";

            AutoMapper.Mapper.Initialize(cfg => {
                cfg.AddProfile<Profile1>();
            });
            return AutoMapper.Mapper.Map<Address>(add);
        }
    }

public class LowerNamingConvention : INamingConvention
    {
        public Regex SplittingExpression
        {
            get { return new Regex(@"[\p{Ll}a-z A-Z 0-9]+(?=_?)"); }
        }

        public string SeparatorCharacter
        {
            get { return string.Empty; }
        }
    }

public class Profile1 : Profile
    {
        protected override void Configure()
        {
            SourceMemberNamingConvention = new LowerNamingConvention();
            DestinationMemberNamingConvention = new PascalCaseNamingConvention();
            CreateMap<adresse, Address>();
        }
    }



[TestFixture]
    public class AddressMapperTest
    {
        [Test]
        public void TestMapper()
        {
            var sut = new AddressMapper();

            var value = sut.map();
        }
    }

When I'm running the test every field in the destination model is null.

As you can see there is a problem with the naming because some names in the source model I have some times different naming conventions like lower case or lower camel case. Does anyone have an idea to solve this problem? Or do I have to map everything manualy?

You should use DataContract and DataMember attributes as below, so that you don't need to give property a same name and also you can follow coding standards.

    [DataContract(Namespace = "")]
    public class YourClass
    {
        [DataMember(EmitDefaultValue = false, Name = "myVariable")]
        public string MyVariable { get; set; }
    }

I think i found a proper solution for my problem. Sorry for not being familar with Regex. I just combined the RegExes from

AutoMapper/src/AutoMapper/PascalCaseNamingConvention.cs and AutoMapper/src/AutoMapper/LowerUnderscoreNamingConvention.cs

into my own naming convention. Might be that there are cases wich can cause issues. But as far as I have tested it works.

public class LowerNamingConvention : INamingConvention
{
    public Regex SplittingExpression
    {
        get { return new Regex(@"[\p{Ll}0-9]+(?=$|\p{Lu}[\p{Ll}0-9])|\p{Lu}?[\p{Ll}0-9]+)"); }
    }

    public string SeparatorCharacter
    {
        get { return string.Empty; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM