Search code examples
c#.net-7.0mappermapperly

How to handle the errors message RMG007 and CS8795 in Mapperly. And reduce to code


In my project I am trying to implement Mapperly so I do that

using Riok.Mapperly.Abstractions;
namespace Application.Profiles;
[Mapper]
public partial class MapperlyMapper
{
    public partial UsersToRoleDto Map(UsersToRole usersToRole);
    public partial UsersToRoleDto Map(UsersToRole usersToRole)
    {
        return new UsersToRoleDto
        {
            User = usersToRole.User.FullName,
            Role = usersToRole.Role.Name
        };
    }
    public partial UsersToRole Map(UsersToRoleDto usersToRoleDto);
    public partial UsersToRole Map(UsersToRoleDto usersToRoleDto)
    {
        return new UsersToRole
        {
            User = new User { FullName = usersToRoleDto.User },
            Role = new Role { Name = usersToRoleDto.Role }
        };
    }
    public partial UserDto Map(User user);
    public partial User Map(UserDto userDto);
}

Now to the error messages if I remove the [Mapper] attribute then I get these error messages:

01-Error CS8795 Partial method 'MapperlyMapper.Map(User)' must have an implementation part because it has accessibility modifiers.
02-Error CS8795 Partial method 'MapperlyMapper.Map(UserDto)' must have an implementation part because it has accessibility modifiers.

on this line of code

public partial UserDto Map(User user);
public partial User Map(UserDto userDto);

But if I added the [Mapper] attribute I get these error messages:

01-Error RMG007 Could not map member Domain.Entities.UsersToRoleDto.Role of type string to Domain.Entities.UsersToRole.Role of type Domain.Entities.Role

02-Error RMG007 Could not map member Domain.Entities.UsersToRoleDto.User of type string to Domain.Entities.UsersToRole.User of type Domain.Entities.User on this line of code

public partial UsersToRole Map(UsersToRoleDto usersToRoleDto);

Would it be possible to solve both errors CS8795 and RMG007 at the same time?
Is there a way to reduce to code, such as using ReverseMap() in AutoMapper?


Solution

  • Thanks to Timothy Makkison I get the answer
    Hey, thanks for using Mapperly. It would help if you shared User, UsersToRoleDto and Role etc but I came up with the following.
    Mapperly can't know that you want path User.FullName of type User to map to path User of type string. the paths dont match. Instead it tries to map both User members together. This fails because it doesn't know how to convert from User -> string. I suggest you use MapProperty docs or define how to convert from string -> User and User -> string

    [Mapper]
    public partial class Mapper
    {
        [MapProperty("User.FullName", "User")]
        [MapProperty("Role.Name", "Role")]
        public partial UsersToRoleDto Map(UsersToRole usersToRole);
    
        [MapProperty("User", "User.FullName")]
        [MapProperty("Role", "Role.Name")]
        public partial UsersToRole Map(UsersToRoleDto usersToRoleDto);
    }