Skip to content
Sergey edited this page Jun 1, 2022 · 10 revisions

Here are all smells

AMA0001

Profile doesn't contain maps

Profile class must contain maps.

Example:

    public class TestProfile : Profile
    {
    }

Fix: Fix is in progress.

AMA0002

Identical names properties are manual mapped

Properties with identical names should not be mapped manually.

Example:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
    }

Fix:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>();
    }

Exception:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForAllOtherMembers(opt => opt.Ignore());
    }

AMA0003

Manual checkind thar src is not null

Null checking for properties with identical names should be done by NullSubstitute call.

Examples:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id ?? 10));
    }
    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id == null ? 10 : src.Id));
    }
    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Device, opt => opt.MapFrom(src => src.Device != null ? src.Device.Name : null));
    }

Fixes:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Id, opt => opt.NullSubstitute(10));
    }
    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Device, opt => {
                opt.NullSubstitute(null); 
                opt.MapFrom(src => src.Device.Name);
            });
    }

AMA0004

ForMember ignore for all left properties

The smell is in progree.

AMA0005

Manual flattening of complex model

Flattening of complex model with naming similar internal properties should be done by IncludeMember call.

Example:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.User.Name))
            .ForMember(dest => dest.SureName, opt => opt.MapFrom(src => src.User.SureName));
    }

Fix:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .IncludeMembers(src => src.User);
    }

AMA0006

Manual flattening of naming similar complex model

Flattening of naming similar complex model will be done automatically.

Example:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>()
            .ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.User.Name));
    }

Fix:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>();
    }

AMA0007

Useless try-catch/finally covering of CreateMap calls

CreateMap into Profile should not be covered by try-catch/finally.

Example:

    public class TestProfile : Profile
    {
        try
        {
            CreateMap<InputObject, OutputObject>();
            Console.WriteLine();
        } 
        catch (Exception e)
        {
           Console.WriteLine(e.Message);
        }
    }

Fix:

    public class TestProfile : Profile
    {
        CreateMap<InputObject, OutputObject>();
        try
        {
            Console.WriteLine();
        } 
        catch (Exception e)
        {
           Console.WriteLine(e.Message);
        }
    }
Clone this wiki locally