Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!
Một pull request liên quan đã được merge.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 68/100
Hướng nghiên cứu
Bắt đầu từ PropertySettingBuilder.Map và tái hiện ví dụ UserCodeGenConfig bằng Mapster.Tool 10.0.12. Truy vết cách đối số mapFunc đi đến đầu ra UserMapper.AdaptToDto được sinh ra; hoàn tất khi FullName được sinh từ cả FirstName và LastName thay vì chỉ từ LastName.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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?
- Ngôn ngữ chính
- C#
- Star
- 5.2k
- Fork
- 411
- Merge trung bình
- 1 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của MapsterMapper/Mapster
-
.NET MatrixĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
MapsterMapper/Mapster#1003 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
MapsterMapper/Mapster#1024 · 1 bình luận ·
-
[Mapster Tool] Add support for global:: to disambiguate type names in generated mappersCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởimprovement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 65/100
MapsterMapper/Mapster#1020 · 5 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 57/100
MapsterMapper/Mapster#1007 · 5 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
MapsterMapper/Mapster#1005 ·
Tất cả issue của MapsterMapper/Mapster
Issue tương tự
-
bug mcp misleading small
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
accessibility help wanted macos
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
dorlugasigal/annotator#42 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
AvaloniaUI/Avalonia#22568 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[bug] 截图失败日志冲爆硬盘Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
babalae/better-genshin-impact#3846 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[辞書] ブルアカ->ブルーアーカイブĐang mở要トリアージ 辞書
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày