From e237a4a47d20830d1de3313d5ac7a75f78b166ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E9=9B=85=E8=99=8E?= Date: Tue, 12 Nov 2024 17:25:12 +0800 Subject: [PATCH] =?UTF-8?q?#4=20=E6=8F=90=E4=BE=9BMapperToPartial=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=87=AA=E5=AE=9A=E4=B9=89Mapper=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutoDtoSourceGenerator.cs | 39 ++++++++++++++++++- .../Dtos/UserDto.cs | 22 +++++------ Biwen.AutoClassGen.TestConsole/Program.cs | 5 +++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs b/Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs index 22c6926..47265d8 100644 --- a/Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs +++ b/Biwen.AutoClassGen.Gen/AutoDtoSourceGenerator.cs @@ -273,6 +273,7 @@ private static void GenSource(Compilation compilation, SourceProductionContext c List namespaces = []; StringBuilder bodyInnerBuilder = new(); StringBuilder mapperBodyBuilder = new(); + StringBuilder mapperBodyBuilder2 = new(); bodyInnerBuilder.AppendLine(); @@ -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},"); + } } }); } @@ -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}"; @@ -398,16 +406,43 @@ namespace $namespace using $ns ; public static partial class $baseclassTo$dtoclassExtentions {{ + /// + /// custom mapper + /// + static partial void MapperToPartial($baseclass from, $dtoclass to); /// /// mapper to $dtoclass /// /// 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 + {{ + /// + /// custom mapper + /// + static partial void MapperToPartial($dtoclass from, $baseclass to); + /// + /// mapper to $baseclass + /// + /// + public static $baseclass MapperTo$baseclass(this $dtoclass model) + {{ + var retn = new $baseclass() + {{ + $2body + }}; + MapperToPartial(model, retn); + return retn; }} }} }} diff --git a/Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs b/Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs index ac1680a..792ac0f 100644 --- a/Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs +++ b/Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs @@ -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 @@ -70,20 +71,15 @@ public partial class VenueDto namespace Biwen.AutoClassGen.TestConsole.Entitys { + public static partial class UserToUserDtoExtentions { - ///// - ///// mapper to UserDto - ///// - ///// - //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"; + } + } + + } \ No newline at end of file diff --git a/Biwen.AutoClassGen.TestConsole/Program.cs b/Biwen.AutoClassGen.TestConsole/Program.cs index 39860a3..9ca5bd8 100644 --- a/Biwen.AutoClassGen.TestConsole/Program.cs +++ b/Biwen.AutoClassGen.TestConsole/Program.cs @@ -106,6 +106,9 @@ var user3Dto = user.MapperToUser3Dto(); +var user2 = user3Dto.MapperToUser(); + + var venue = new Venue { Address = "No1 street", @@ -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(); \ No newline at end of file