Skip to content

Commit

Permalink
#4 提供MapperToPartial 用于自定义Mapper实现
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 12, 2024
1 parent 3c4a918 commit e237a4a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
39 changes: 37 additions & 2 deletions Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ private static void GenSource(Compilation compilation, SourceProductionContext c
List<string> namespaces = [];
StringBuilder bodyInnerBuilder = new();
StringBuilder mapperBodyBuilder = new();
StringBuilder mapperBodyBuilder2 = new();

bodyInnerBuilder.AppendLine();

Expand Down Expand Up @@ -318,11 +319,17 @@ void GenProperty(TypeSyntax @type)
bodyInnerBuilder.AppendLine($"{raw}");

// mapper:
// 只有public的属性才能赋值
// 只有public的属性才能赋值.且可写
if (prop.GetMethod?.DeclaredAccessibility == Accessibility.Public)
{
mapperBodyBuilder.AppendLine($"{prop.Name} = model.{prop.Name},");
}
// mapper2:
if (prop.GetMethod?.DeclaredAccessibility == Accessibility.Public &&
prop.SetMethod?.DeclaredAccessibility == Accessibility.Public)
{
mapperBodyBuilder2.AppendLine($"{prop.Name} = model.{prop.Name},");
}
}
});
}
Expand Down Expand Up @@ -365,6 +372,7 @@ void GenProperty(TypeSyntax @type)
mapperSource = mapperSource.Replace("$baseclass", metadata.FromClass);
mapperSource = mapperSource.Replace("$dtoclass", className);
mapperSource = mapperSource.Replace("$body", mapperBodyBuilder.ToString());
mapperSource = mapperSource.Replace("$2body", mapperBodyBuilder2.ToString());

// 合并
source = $"{source}\r\n{mapperSource}";
Expand Down Expand Up @@ -398,16 +406,43 @@ namespace $namespace
using $ns ;
public static partial class $baseclassTo$dtoclassExtentions
{{
/// <summary>
/// custom mapper
/// </summary>
static partial void MapperToPartial($baseclass from, $dtoclass to);
/// <summary>
/// mapper to $dtoclass
/// </summary>
/// <returns></returns>
public static $dtoclass MapperTo$dtoclass(this $baseclass model)
{{
return new $dtoclass()
var retn = new $dtoclass()
{{
$body
}};
MapperToPartial(model, retn);
return retn;
}}
}}
public static partial class $dtoclassTo$baseclassExtentions
{{
/// <summary>
/// custom mapper
/// </summary>
static partial void MapperToPartial($dtoclass from, $baseclass to);
/// <summary>
/// mapper to $baseclass
/// </summary>
/// <returns></returns>
public static $baseclass MapperTo$baseclass(this $dtoclass model)
{{
var retn = new $baseclass()
{{
$2body
}};
MapperToPartial(model, retn);
return retn;
}}
}}
}}
Expand Down
22 changes: 9 additions & 13 deletions Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Test Console App UserDto.cs
// 2024-09-26 16:17:44 Biwen.AutoClassGen.TestConsole 万雅虎

using Biwen.AutoClassGen.TestConsole.Dtos;
using Biwen.AutoClassGen.TestConsole.Entitys;

namespace Biwen.AutoClassGen.TestConsole.Dtos
Expand Down Expand Up @@ -70,20 +71,15 @@ public partial class VenueDto

namespace Biwen.AutoClassGen.TestConsole.Entitys
{

public static partial class UserToUserDtoExtentions
{
///// <summary>
///// mapper to UserDto
///// </summary>
///// <returns></returns>
//public static UserDto MapperToDto(this User model)
//{
// return new UserDto()
// {
// FirstName = model.FirstName,
// LastName = model.LastName,
// Age = model.Age,
// };
//}
static partial void MapperToPartial(User from, UserDto to)
{
to.FirstName = "重写了FirstName";
}

}


}
5 changes: 5 additions & 0 deletions Biwen.AutoClassGen.TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
var user3Dto = user.MapperToUser3Dto();


var user2 = user3Dto.MapperToUser();


var venue = new Venue
{
Address = "No1 street",
Expand All @@ -123,10 +126,12 @@
Console.WriteLine($"I`m {nameof(user2Dto)} {user2Dto.FirstName} {user2Dto.LastName} I`m {user2Dto.Age} years old");
Console.WriteLine($"I`m {nameof(user3Dto)} {user3Dto.FirstName} {user3Dto.LastName} I`m {user3Dto.Age} years old");

Console.WriteLine($"I`m {nameof(user)}2 {user2.FirstName} {user2.LastName} I`m {user2.Age} years old");

Console.WriteLine($"I`m {nameof(venuesDto)} {venuesDto.Name} {venuesDto.Address} I have {venuesDto.Images?.Count} images");





Console.ReadLine();

0 comments on commit e237a4a

Please sign in to comment.