-
Notifications
You must be signed in to change notification settings - Fork 0
Smells
Here are all smells
Profile doesn't contain maps
Profile class must contain maps.
Example:
public class TestProfile : Profile
{
}
Fix: Fix is in progress.
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());
}
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);
});
}
ForMember ignore for all left properties
The smell is in progree.
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);
}
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>();
}
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);
}
}