AutoMapper

This commit is contained in:
Stef Heyenrath
2021-07-25 15:26:50 +02:00
parent 3c9a292a0c
commit 2531e8e688
9 changed files with 264 additions and 96 deletions
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -12,6 +14,41 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
_context = context;
}
protected string GetPropertyType(IPropertySymbol property, out Dictionary<string, string> differs)
{
differs = new Dictionary<string, string>();
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
if (existing is not null)
{
differs.Add(property.Type.ToString(), existing.InterfaceName);
return existing.InterfaceName;
}
if (property.Type is INamedTypeSymbol namedTypedSymbol)
{
var type = property.Type.ToString();
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgument.ToString());
if (exist is not null)
{
if (!differs.ContainsKey(typeArgument.ToString()))
{
differs.Add(typeArgument.ToString(), exist.InterfaceName);
}
type = type.Replace(typeArgument.ToString(), exist.InterfaceName);
}
}
return type;
}
return property.Type.ToString();
}
protected INamedTypeSymbol GetType(string name)
{
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
@@ -94,16 +94,19 @@ namespace {symbol.ContainingNamespace}
//}
//else
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
if (existing is not null)
{
str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}");
}
else
{
str.AppendLine($" {property.ToPropertyText()}");
}
var type = GetPropertyType(property, out _);
str.AppendLine($" {property.ToPropertyText(type)}");
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
//if (existing is not null)
//{
// str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}");
//}
//else
//{
// str.AppendLine($" {property.ToPropertyText()}");
//}
str.AppendLine();
}
@@ -43,16 +43,21 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
}
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
using AutoMapper;
namespace {symbol.ContainingNamespace}
{{
public class {className}Proxy : {interfaceName}
{{
private readonly IMapper _mapper;
public {className} _Instance {{ get; }}
public {className}Proxy({className} instance)
{{
_Instance = instance;
{GenerateAutoMapper()}
}}
{GeneratePublicProperties(symbol, proxyAll)}
@@ -61,6 +66,22 @@ namespace {symbol.ContainingNamespace}
}}
}}";
private string GenerateAutoMapper()
{
var str = new StringBuilder();
str.AppendLine(" _mapper = new MapperConfiguration(cfg =>");
str.AppendLine(" {");
foreach (var x in _context.CandidateInterfaces)
{
str.AppendLine($" cfg.CreateMap<{x.Value.InterfaceName}, {x.Value.ClassName}>();");
str.AppendLine($" cfg.CreateMap<{x.Value.ClassName}, {x.Value.InterfaceName}>();");
}
str.AppendLine(" }).CreateMapper();");
return str.ToString();
}
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
@@ -82,15 +103,51 @@ namespace {symbol.ContainingNamespace}
// ComplexProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex))
{
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
if (existing is not null)
{
str.AppendLine($" public {property.ToPropertyTextForClass(existing.InterfaceName, existing.ClassName)}");
}
else
var type = GetPropertyType(property, out var differs);
if (!differs.Any())
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
}
else
{
str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
// var get = property.GetMethod != null ? $"get => _mapper.Map<{type}>(_Instance.{property.Name}); " : string.Empty;
// var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = _mapper.Map<{property.Type}>(value);" : string.Empty;
//var p = $"{type} {property.Name} {{ {get}{set}}}";
//str.AppendLine($" public {type} {property.Name} {{ {get}{set}}}");
}
/*
public IList<IClazz> Cs
{
get => _mapper.Map<IList<IClazz>>(_instance.Cs);
set => _instance.Cs = _mapper.Map<IList<Clazz>>(value);
}
}*/
//public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
//{
// // var classNameProxy = $"Proxy";
// var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.Name}); " : string.Empty;
// var set = property.SetMethod != null ? $"set => _mapper.Map<{property.Type}>( = (({classNameProxy}) value)._Instance; " : string.Empty;
// return $"{overrideType} {property.Name} {{ {get}{set}}}";
//}
//str.AppendLine($" public {property.ToPropertyTextForClass(type)}");
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == property.Type.ToString());
//if (existing is not null)
//{
// str.AppendLine($" public {property.ToPropertyTextForClass(existing.InterfaceName, existing.ClassName)}");
//}
//else
//{
// str.AppendLine($" public {property.ToPropertyTextForClass()}");
//}
str.AppendLine();
}