Skip to content

Commit

Permalink
support Mapper entity to dto extentions #4
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 5, 2023
1 parent adf028e commit 2b877a9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
49 changes: 49 additions & 0 deletions Biwen.AutoClassGen.Gen/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ private static void HandleAnnotatedNodesDto(Compilation compilation, ImmutableAr
List<string> namespaces = [];
StringBuilder bodyInnerBuilder = new();

StringBuilder mapperBodyBuilder = new();


//生成属性
void genProperty(TypeSyntax @type)
{
Expand All @@ -322,6 +325,9 @@ void genProperty(TypeSyntax @type)
//body:
bodyInnerBuilder.AppendLine($"/// <inheritdoc cref=\"{@type}.{prop.Name}\" />");
bodyInnerBuilder.AppendLine($"{raw}");

//mapper:
mapperBodyBuilder.AppendLine($"{prop.Name} = model.{prop.Name},");
}
});
}
Expand Down Expand Up @@ -354,11 +360,54 @@ void genProperty(TypeSyntax @type)
source = source.Replace("$ni", rootNamespace);
//format:
source = FormatContent(source);


//生成Mapper
var mapperSource = MapperTemplate.Replace("$namespace", namespaces.First());
mapperSource = mapperSource.Replace("$ns", rootNamespace);
mapperSource = mapperSource.Replace("$baseclass", entityName);
mapperSource = mapperSource.Replace("$dtoclass", className);
mapperSource = mapperSource.Replace("$body", mapperBodyBuilder.ToString());


mapperSource = FormatContent(mapperSource);

//合并
source = source + Environment.NewLine + mapperSource;

context.AddSource($"Biwen.AutoClassGenDto.{className}.{node.Identifier.Text}.g.cs", SourceText.From(source, Encoding.UTF8));
}
}
}


#region Template

public static readonly string MapperTemplate = $@"
#pragma warning disable
namespace $namespace
{{
using $ns ;
public static partial class $baseclassTo$dtoclassExtentions
{{
/// <summary>
/// mapper to $dtoclass
/// </summary>
/// <returns></returns>
public static $dtoclass MapperToDto(this $baseclass model)
{{
return new $dtoclass()
{{
$body
}};
}}
}}
}}
#pragma warning restore
";

#endregion

/// <summary>
/// 格式化代码
/// </summary>
Expand Down
25 changes: 24 additions & 1 deletion Biwen.AutoClassGen.TestConsole/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Biwen.AutoClassGen.TestConsole.Entitys;
using Biwen.AutoClassGen.TestConsole.Dtos;
using Biwen.AutoClassGen.TestConsole.Entitys;
using System.Runtime.CompilerServices;


#pragma warning disable
Expand All @@ -11,7 +13,28 @@ namespace Biwen.AutoClassGen.TestConsole.Dtos
[AutoDto(typeof(User), nameof(User.Id), "TestCol")]
public partial class UserDto
{
}
}

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,
// };
//}
}
}


#pragma warning restore
16 changes: 15 additions & 1 deletion Biwen.AutoClassGen.TestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Biwen.AutoClassGen;
using Biwen.AutoClassGen.TestConsole.Entitys;


// See https://aka.ms/new-console-template for more information
Expand All @@ -11,13 +12,26 @@
KeyWord = "biwen"
};

Biwen.AutoClassGen.TestConsole.Dtos.UserDto userDto = new()
Biwen.AutoClassGen.TestConsole.Dtos.UserDto userDto2 = new()
{
FirstName = "biwen",
LastName = "wan",
Age = 18,
};

var user = new User
{
Age = 18,
Email = "[email protected]",
FirstName = "biwen",
LastName = "wan",
Id = "001",
Remark = "this is a test",
};

//mapper to UserDto
var userDto = user.MapperToDto();


Console.WriteLine($"{queryRequest.KeyWord}");
Console.WriteLine($"I`m {userDto.FirstName} {userDto.LastName} I`m {userDto.Age} years old");
Expand Down

0 comments on commit 2b877a9

Please sign in to comment.