Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ProjectR/Builders/CodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@

private void BuildProjectAsMethod(INamedTypeSymbol mapperSymbol, MappingPlan plan)
{
// For Project: plan.SourceType = Entity, plan.DestinationType = DTO
// Generates: TDto Project(TEntity source)
var sourceTypeName = plan.SourceType.ToDisplayString();
var destTypeName = plan.DestinationType.ToDisplayString();
BuildXmlDoc(mapperSymbol, "Project");
Expand All @@ -106,6 +108,8 @@

private void BuildBuildMethod(INamedTypeSymbol mapperSymbol, MappingPlan plan)
{
// For Build: plan.SourceType = DTO, plan.DestinationType = Entity
// Generates: TEntity Build(TDto source)
var sourceTypeName = plan.SourceType.ToDisplayString();
var destTypeName = plan.DestinationType.ToDisplayString();
BuildXmlDoc(mapperSymbol, "Build");
Expand All @@ -130,11 +134,11 @@
switch (plan.Creation.Method)
{
case CreationMethod.ConstructorWithParameters:
var constructorArgs = GetMethodArguments(plan.Creation.Constructor, plan.Creation, sourceVarName);

Check warning on line 137 in ProjectR/Builders/CodeBuilder.cs

View workflow job for this annotation

GitHub Actions / build_and_publish

Possible null reference argument for parameter 'method' in 'string CodeBuilder.GetMethodArguments(IMethodSymbol method, CreationInfo creationInfo, string sourceVarName)'.

Check warning on line 137 in ProjectR/Builders/CodeBuilder.cs

View workflow job for this annotation

GitHub Actions / build_and_publish

Possible null reference argument for parameter 'method' in 'string CodeBuilder.GetMethodArguments(IMethodSymbol method, CreationInfo creationInfo, string sourceVarName)'.
_sb.AppendLine($"new {variableType.ToDisplayString()}({constructorArgs})");
break;
case CreationMethod.FactoryMethod:
var factoryArgs = GetMethodArguments(plan.Creation.FactoryMethod, plan.Creation, sourceVarName);

Check warning on line 141 in ProjectR/Builders/CodeBuilder.cs

View workflow job for this annotation

GitHub Actions / build_and_publish

Possible null reference argument for parameter 'method' in 'string CodeBuilder.GetMethodArguments(IMethodSymbol method, CreationInfo creationInfo, string sourceVarName)'.

Check warning on line 141 in ProjectR/Builders/CodeBuilder.cs

View workflow job for this annotation

GitHub Actions / build_and_publish

Possible null reference argument for parameter 'method' in 'string CodeBuilder.GetMethodArguments(IMethodSymbol method, CreationInfo creationInfo, string sourceVarName)'.
_sb.AppendLine($"{variableType.ToDisplayString()}.{plan.Creation.FactoryMethod.Name}({factoryArgs})");
break;
case CreationMethod.ParameterlessConstructor:
Expand Down Expand Up @@ -270,7 +274,7 @@

private void BuildXmlDoc(INamedTypeSymbol mapperSymbol, string methodName)
{
var baseMethod = mapperSymbol.BaseType.GetMembers(methodName).OfType<IMethodSymbol>().FirstOrDefault();

Check warning on line 277 in ProjectR/Builders/CodeBuilder.cs

View workflow job for this annotation

GitHub Actions / build_and_publish

Dereference of a possibly null reference.
if (baseMethod?.GetDocumentationCommentXml() is string xml && !string.IsNullOrEmpty(xml))
{
var lines = xml.Split(new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries).Select(l => l.Trim());
Expand Down
18 changes: 11 additions & 7 deletions ProjectR/Mapping/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ private static void ProcessMapperClass(Compilation compilation, ClassDeclaration
var baseType = mapperSymbol.BaseType;
if (baseType == null || baseType.TypeArguments.Length != 2) return;

var sourceType = baseType.TypeArguments[0];
var destinationType = baseType.TypeArguments[1];
// Mapper<TEntity, TDto> type arguments
var entityType = baseType.TypeArguments[0]; // TEntity
var dtoType = baseType.TypeArguments[1]; // TDto

var allUsings = new HashSet<string>();
CollectUsings(mapperSymbol, allUsings);
CollectUsings(sourceType, allUsings);
CollectUsings(destinationType, allUsings);
CollectUsings(entityType, allUsings);
CollectUsings(dtoType, allUsings);

var policyMethodSyntax = mapperSymbol.GetMembers("ConfigureMappingPolicies")
.FirstOrDefault(m => m.IsStatic && !m.IsImplicitlyDeclared)?
Expand All @@ -176,9 +177,12 @@ private static void ProcessMapperClass(Compilation compilation, ClassDeclaration
// Use the provided compilation which includes placeholders
var engine = new PolicyEngine(compilation, policyMethodSyntax, allMappers);

var projectAsPlan = engine.CreateProjectAsPlan(sourceType, destinationType);
var buildPlan = engine.CreateBuildPlan(destinationType, sourceType);
var applyToPlan = engine.CreateApplyToPlan(destinationType, sourceType);
// Project: Entity -> DTO
var projectAsPlan = engine.CreateProjectAsPlan(entityType, dtoType);
// Build: DTO -> Entity
var buildPlan = engine.CreateBuildPlan(dtoType, entityType);
// ApplyTo: DTO -> Entity (modification)
var applyToPlan = engine.CreateApplyToPlan(dtoType, entityType);

projectAsPlan.Diagnostics.ForEach(context.ReportDiagnostic);
buildPlan.Diagnostics.ForEach(context.ReportDiagnostic);
Expand Down
4 changes: 2 additions & 2 deletions ProjectR/Policies/Policies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public interface IPolicyConfiguration
/// <summary>
/// Configures the policy for the Build(TDto) method.
/// </summary>
IBuildPolicyBuilder<TDestination, TSource> ForCreation<TDestination, TSource>();
IBuildPolicyBuilder<TSource, TDestination> ForCreation<TSource, TDestination>();

/// <summary>
/// Configures the policy for the Apply(TDto, TEntity) method.
/// </summary>
IApplyToPolicyBuilder<TDestination, TSource> ForModification<TDestination, TSource>();
IApplyToPolicyBuilder<TSource, TDestination> ForModification<TSource, TDestination>();
}

// NUOVA INTERFACCIA DI BASE PER LE CONFIGURAZIONI A LIVELLO DI PROPRIETÀ
Expand Down
8 changes: 4 additions & 4 deletions ProjectR/Policies/PoliciesApiInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public IProjectAsPolicyBuilder<TSource, TDestination> ForProjection<TSource, TDe
return new ProjectAsPolicyBuilder<TSource, TDestination>();
}

public IBuildPolicyBuilder<TDestination, TSource> ForCreation<TDestination, TSource>()
public IBuildPolicyBuilder<TSource, TDestination> ForCreation<TSource, TDestination>()
{
return new BuildPolicyBuilder<TDestination, TSource>();
return new BuildPolicyBuilder<TSource, TDestination>();
}

public IApplyToPolicyBuilder<TDestination, TSource> ForModification<TDestination, TSource>()
public IApplyToPolicyBuilder<TSource, TDestination> ForModification<TSource, TDestination>()
{
return new ApplyToPolicyBuilder<TDestination, TSource>();
return new ApplyToPolicyBuilder<TSource, TDestination>();
}
}

Expand Down
Loading