Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!
评估
调研方向
从 PropertySettingBuilder.Map 开始,使用 Mapster.Tool 10.0.12 重现 UserCodeGenConfig 示例。跟踪 mapFunc 参数如何传递到生成的 UserMapper.AdaptToDto 输出中;当 FullName 是由 FirstName 和 LastName 共同生成,而不是仅由 LastName 生成时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
I'm using -
Mapster 10.0.12
Mapster.Tool 10.0.12
.NET 10.0.400
I have the following entity model -
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
and I wanted the following DTO model -
public partial class UserDto
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}
and a mapper extension method to be generated which would map User.FirstName + " " + User.LastName to UserDto.FullName.
I used the following code -
public class UserCodeGenConfig : ICodeGenerationRegister
{
public void Register(CodeGenerationConfig config)
{
config.AdaptTo("[name]Dto", MapType.Map)
.ForType<User>(p =>
{
p.Ignore(s => s.FirstName);
p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");
});
config.GenerateMapper("[name]Mapper")
.ForType<User>();
}
}
and used the model and extension generating commands with Mapster.Tool. Following are the generated codes -
public partial class UserDto
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}
public static partial class UserMapper
{
public static UserDto AdaptToDto(this User p1)
{
return p1 == null ? null : new UserDto()
{
Id = p1.Id,
Email = p1.Email,
FullName = p1.LastName,
Age = p1.Age
};
}
}
As you can see, the DTO model is generated as expected, but the mapping code is exactly what it would generate if I had used -
p.Map(s => s.LastName, "FullName");
instead of -
p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");
Following is the source code documentation for the Map method in question -
/// <summary>
/// Map a specific property of the source type to a target property using a custom mapping function.
/// </summary>
/// <typeparam name="TReturn">Type of source property.</typeparam>
/// <typeparam name="TReturn2">Type of target property type.</typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="mapFunc">A lambda expression that defines the custom mapping function.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null);
So, it seems the tool is not respecting the 2nd parameter (Expression<Func<T, TReturn2>> mapFunc) of this Map method.
Is there anything I failed to understand or is this a bug? Is there any other approach to achieve the expected result?
- 主要语言
- C#
- 星标
- 5.2k
- 派生
- 411
- 平均合并
- 3 天 4 小时
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MapsterMapper/Mapster 的其他 Issue
-
.NET Matrix未关闭
难度 2/5 1-3 小时 新手友好度 72/100
MapsterMapper/Mapster#1003 ·
-
难度 3/5 1-2 天 新手友好度 58/100
MapsterMapper/Mapster#1024 · 1 条评论 ·
-
[Mapster Tool] Add support for global:: to disambiguate type names in generated mappers可能已有人在做 @DocSvartz 于 3 天前认领。 未关闭improvement
难度 3/5 1-2 天 新手友好度 65/100
MapsterMapper/Mapster#1020 · 5 条评论 ·
-
难度 3/5 1-2 天 新手友好度 57/100
MapsterMapper/Mapster#1007 · 5 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
MapsterMapper/Mapster#1005 ·
查看 MapsterMapper/Mapster 的全部 Issue
相似的 Issue
-
area: client bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug mcp misleading small
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
Bug Platform: Android
难度 2/5 1-3 小时 新手友好度 78/100
getsentry/sentry-dotnet#5764 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/SimpleL7Proxy#287 ·
维护者通常 1 天内回复