Skip to content

Commit 9cba85d

Browse files
committed
Checking for installed mappers are more flexible. Also cleaned up some code.
1 parent 3330ba7 commit 9cba85d

11 files changed

+116
-117
lines changed

Modules/Intent.Modules.Application.DomainInteractions/Extensions/DomainInteractionExtensions.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ public static IEnumerable<CSharpStatement> GetReturnStatements(this CSharpClassM
288288
}
289289
var statements = new List<CSharpStatement>();
290290

291-
var mapperIsInstalled = method.File.Template.ExecutionContext.InstalledModules.Any(x => x.ModuleId == "Intent.Application.Dtos.AutoMapper"
292-
|| x.ModuleId == "Intent.Application.Dtos.Mapperly");
291+
var mapperIsInstalled = MappingStrategyProvider.Instance.HasMappingStrategy(method);
293292

294293
var template = method.File.Template;
295294
var entitiesReturningPk = GetEntitiesReturningPk(method, returnType);
@@ -303,7 +302,7 @@ public static IEnumerable<CSharpStatement> GetReturnStatements(this CSharpClassM
303302
var entityDetails = method.TrackedEntities().Values.First(x => x.ElementModel?.Id == returnType.Element.AsDTOModel().Mapping.ElementId);
304303
if (entityDetails.ProjectedType == returnDto)
305304
{
306-
statements.Add($"return {entityDetails.VariableName};");
305+
statements.Add(new CSharpReturnStatement(entityDetails.VariableName));
307306
}
308307
else
309308
{
@@ -322,14 +321,13 @@ public static IEnumerable<CSharpStatement> GetReturnStatements(this CSharpClassM
322321
var entityDetails = method.TrackedEntities().Values.First(x => x.ElementModel.Id == returnType.GenericTypeParameters.First().Element.AsDTOModel().Mapping.ElementId);
323322
if (entityDetails.ProjectedType == returnDto)
324323
{
325-
statements.Add($"return {entityDetails.VariableName}.{mappingMethod}();");
324+
statements.Add(new CSharpReturnStatement($"{entityDetails.VariableName}.{mappingMethod}()"));
326325
}
327326
else
328327
{
329328

330329
var mapper = MappingStrategyProvider.Instance.GetMappingStrategy(method)!;
331330
mapper.ImplementPagedMappingStatement(method, statements, entityDetails, template, returnType, returnDto, mappingMethod);
332-
//statements.Add($"return {entityDetails.VariableName}.{mappingMethod}(x => x.MapTo{returnDto}({autoMapperFieldName}));");
333331
}
334332
}
335333
else if (returnType.Element.IsTypeDefinitionModel() && (nonUserSuppliedEntitiesReturningPks.Count == 1 || entitiesReturningPk.Count == 1)) // No need for TrackedEntities thus no check for it
@@ -338,12 +336,12 @@ public static IEnumerable<CSharpStatement> GetReturnStatements(this CSharpClassM
338336
? nonUserSuppliedEntitiesReturningPks[0]
339337
: entitiesReturningPk[0];
340338
var entity = entityDetails.ElementModel.AsClassModel();
341-
statements.Add($"return {entityDetails.VariableName}.{entity.GetTypesInHierarchy().SelectMany(x => x.Attributes).FirstOrDefault(x => x.IsPrimaryKey(isUserSupplied: false))?.Name ?? "Id"};");
339+
statements.Add(new CSharpReturnStatement($"{entityDetails.VariableName}.{entity.GetTypesInHierarchy().SelectMany(x => x.Attributes).FirstOrDefault(x => x.IsPrimaryKey(isUserSupplied: false))?.Name ?? "Id"}"));
342340
}
343341
else if (method.TrackedEntities().Values.Any(x => returnType.Element.Id == x.ElementModel.Id))
344342
{
345343
var entityDetails = method.TrackedEntities().Values.First(x => returnType.Element.Id == x.ElementModel.Id);
346-
statements.Add($"return {entityDetails.VariableName};");
344+
statements.Add(new CSharpReturnStatement(entityDetails.VariableName));
347345
}
348346
else
349347
{

Modules/Intent.Modules.Application.DomainInteractions/Intent.Application.DomainInteractions.imodspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<package>
33
<id>Intent.Application.DomainInteractions</id>
4-
<version>1.1.7-pre.0</version>
4+
<version>1.1.7-pre.1</version>
55
<supportedClientVersions>[4.5.15-a,5.0.0)</supportedClientVersions>
66
<summary>Provides interaction strategies to generate domain interaction implementations.</summary>
77
<description>Provides interaction strategies to generate domain interaction implementations.</description>

Modules/Intent.Modules.Application.DomainInteractions/Intent.Metadata/Module Builder/Intent.Application.DomainInteractions/Intent.Application.DomainInteractions.pkg.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<definitionPackageName>Intent.ModuleBuilder</definitionPackageName>
3434
<definitionPackageId>9972b2a9-b749-4bba-b5c8-824bf694c6ef</definitionPackageId>
3535
<properties>
36-
<property name="4bb9695b-6004-46e1-acea-c48c60c5f8ce" display="Version" value="1.1.6" isActive="true" />
36+
<property name="4bb9695b-6004-46e1-acea-c48c60c5f8ce" display="Version" value="1.1.7-pre.1" isActive="true" />
3737
<property name="177a2415-e749-46e7-8257-440e19ecfb5e" display="API Namespace" value="Intent.Modules.Application.Contracts.Api" isActive="true" />
3838
<property name="0d2ae582-090e-42d6-a7d6-689144433254" display="NuGet Package Id" isActive="true" />
3939
<property name="27cf6544-a9a2-4992-9fef-51d0ed49e66e" display="NuGet Package Version" value="" isActive="true" />

Modules/Intent.Modules.Application.DomainInteractions/Intent.Modules.Application.DomainInteractions.application.output.config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<outputFiles>
33
<files>
44
<file relativePath="Intent.Modules.Application.DomainInteractions/Intent.Modules.Application.DomainInteractions.csproj" state="ignored" />
5-
<file relativePath="Intent.Modules.Application.DomainInteractions/release-notes.md" state="once-off-generated" />
5+
<file relativePath="Intent.Modules.Application.DomainInteractions/release-notes.md" state="ignored;once-off-generated" />
66
</files>
77
</outputFiles>

Modules/Intent.Modules.Application.DomainInteractions/Intent.Modules.Application.DomainInteractions.application.output.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<OverwriteBehaviour>once-off</OverwriteBehaviour>
3939
<ApplicationRelativeFilePath>release-notes.md</ApplicationRelativeFilePath>
4040
<ProjectRelativeFilePath>release-notes.md</ProjectRelativeFilePath>
41-
<IsIgnored>false</IsIgnored>
41+
<IsIgnored>true</IsIgnored>
4242
</FileLog>
4343
</FileLogs>
4444
</outputLog>

Modules/Intent.Modules.Application.DomainInteractions/Intent.Modules.Application.DomainInteractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<Version>1.1.7-pre.0</Version>
5+
<Version>1.1.7-pre.1</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Intent.Metadata.Models;
4+
using Intent.Modules.Common.CSharp.Builder;
5+
using Intent.Modules.Common.CSharp.Interactions;
6+
using Intent.Modules.Common.CSharp.Templates;
7+
8+
namespace Intent.Modules.Application.DomainInteractions.Strategies;
9+
10+
internal class AutoMapperMappingStrategy : IMappingStrategy
11+
{
12+
public bool IsMatch(ICSharpClassMethodDeclaration method)
13+
{
14+
return method.File.Template.ExecutionContext.InstalledModules.Any(x => x.ModuleId == "Intent.Application.Dtos.AutoMapper");
15+
}
16+
17+
public void ImplementMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements,
18+
EntityDetails entity, ICSharpTemplate template, ITypeReference returnType, string? returnDto)
19+
{
20+
//Adding Using Clause for Extension Methods
21+
template.TryGetTypeName("Intent.Application.Dtos.EntityDtoMappingExtensions", returnType.Element, out _);
22+
var autoMapperFieldName = method.Class.InjectService(template.UseType("AutoMapper.IMapper"));
23+
var nullable = returnType.IsNullable ? "?" : "";
24+
statements.Add($"return {entity.VariableName}{nullable}.MapTo{returnDto}{(returnType.IsCollection ? "List" : "")}({autoMapperFieldName});");
25+
}
26+
27+
public void ImplementPagedMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements, EntityDetails entity,
28+
ICSharpTemplate template, ITypeReference returnType, string? returnDto, string? mappingMethod)
29+
{
30+
var autoMapperFieldName = method.Class.InjectService(template.UseType("AutoMapper.IMapper"));
31+
statements.Add($"return {entity.VariableName}.{mappingMethod}(x => x.MapTo{returnDto}({autoMapperFieldName}));");
32+
}
33+
34+
public bool HasProjectTo() => true;
35+
}

Modules/Intent.Modules.Application.DomainInteractions/Strategies/IMappingStrategy.cs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using Intent.Modules.Common.CSharp.Builder;
33
using Intent.Modules.Common.CSharp.Interactions;
44
using Intent.Modules.Common.CSharp.Templates;
5-
using System;
65
using System.Collections.Generic;
7-
using System.Linq;
86
using System.Text;
97
using System.Threading.Tasks;
108

@@ -21,60 +19,4 @@ void ImplementMappingStatement(ICSharpClassMethodDeclaration method, List<CSharp
2119
void ImplementPagedMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements, EntityDetails entity, ICSharpTemplate template,
2220
ITypeReference returnType, string? returnDto, string? mappingMethod);
2321
}
24-
25-
public class AutoMapperMappingStrategy : IMappingStrategy
26-
{
27-
public bool IsMatch(ICSharpClassMethodDeclaration method)
28-
{
29-
return method.File.Template.ExecutionContext.InstalledModules.Any(x => x.ModuleId == "Intent.Application.Dtos.AutoMapper");
30-
}
31-
32-
public void ImplementMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements,
33-
EntityDetails entity, ICSharpTemplate template, ITypeReference returnType, string? returnDto)
34-
{
35-
//Adding Using Clause for Extension Methods
36-
template.TryGetTypeName("Intent.Application.Dtos.EntityDtoMappingExtensions", returnType.Element, out _);
37-
var autoMapperFieldName = method.Class.InjectService(template.UseType("AutoMapper.IMapper"));
38-
var nullable = returnType.IsNullable ? "?" : "";
39-
statements.Add($"return {entity.VariableName}{nullable}.MapTo{returnDto}{(returnType.IsCollection ? "List" : "")}({autoMapperFieldName});");
40-
}
41-
42-
public void ImplementPagedMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements, EntityDetails entity,
43-
ICSharpTemplate template, ITypeReference returnType, string? returnDto, string? mappingMethod)
44-
{
45-
var autoMapperFieldName = method.Class.InjectService(template.UseType("AutoMapper.IMapper"));
46-
statements.Add($"return {entity.VariableName}.{mappingMethod}(x => x.MapTo{returnDto}({autoMapperFieldName}));");
47-
}
48-
49-
public bool HasProjectTo() => true;
50-
}
51-
52-
public class MapperlyMappingStrategy : IMappingStrategy
53-
{
54-
public bool IsMatch(ICSharpClassMethodDeclaration method)
55-
{
56-
return method.File.Template.ExecutionContext.InstalledModules.Any(x => x.ModuleId == "Intent.Application.Dtos.Mapperly");
57-
}
58-
59-
public void ImplementMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements,
60-
EntityDetails entity, ICSharpTemplate template, ITypeReference returnType, string? returnDto)
61-
{
62-
var nullable = returnType.IsNullable ? "?" : "";
63-
template.TryGetTypeName("Intent.Application.Dtos.Mapperly.DtoMappingProfile", returnType.Element, out var _);
64-
statements.Add($"var mapper = new {entity.ElementModel.Name}DtoMapper();");
65-
statements.Add($"return mapper.{entity.ElementModel.Name}To{returnDto}{(returnType.IsCollection ? "List" : "")}({entity.VariableName}{(returnType.IsCollection ? ".ToList()" : "")});");
66-
}
67-
68-
public void ImplementPagedMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements, EntityDetails entity,
69-
ICSharpTemplate template, ITypeReference returnType, string? returnDto, string? mappingMethod)
70-
{
71-
template.TryGetTypeName("Intent.Application.Dtos.Mapperly.DtoMappingProfile", returnType.Element, out var _);
72-
statements.Add($"var mapper = new {entity.ElementModel.Name}DtoMapper();");
73-
statements.Add($"return {entity.VariableName}.{mappingMethod}(x => mapper.{entity.ElementModel.Name}To{returnDto}(x));");
74-
return;
75-
throw new NotImplementedException();
76-
}
77-
78-
public bool HasProjectTo() => false;
79-
}
8022
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Intent.Metadata.Models;
5+
using Intent.Modules.Common.CSharp.Builder;
6+
using Intent.Modules.Common.CSharp.Interactions;
7+
using Intent.Modules.Common.CSharp.Templates;
8+
9+
namespace Intent.Modules.Application.DomainInteractions.Strategies;
10+
11+
internal class MapperlyMappingStrategy : IMappingStrategy
12+
{
13+
public bool IsMatch(ICSharpClassMethodDeclaration method)
14+
{
15+
return method.File.Template.ExecutionContext.InstalledModules.Any(x => x.ModuleId == "Intent.Application.Dtos.Mapperly");
16+
}
17+
18+
public void ImplementMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements,
19+
EntityDetails entity, ICSharpTemplate template, ITypeReference returnType, string? returnDto)
20+
{
21+
template.TryGetTypeName("Intent.Application.Dtos.Mapperly.DtoMappingProfile", returnType.Element, out var _);
22+
statements.Add($"var mapper = new {entity.ElementModel.Name}DtoMapper();");
23+
statements.Add($"return mapper.{entity.ElementModel.Name}To{returnDto}{(returnType.IsCollection ? "List" : "")}({entity.VariableName}{(returnType.IsCollection ? ".ToList()" : "")});");
24+
}
25+
26+
public void ImplementPagedMappingStatement(ICSharpClassMethodDeclaration method, List<CSharpStatement> statements, EntityDetails entity,
27+
ICSharpTemplate template, ITypeReference returnType, string? returnDto, string? mappingMethod)
28+
{
29+
template.TryGetTypeName("Intent.Application.Dtos.Mapperly.DtoMappingProfile", returnType.Element, out var _);
30+
statements.Add($"var mapper = new {entity.ElementModel.Name}DtoMapper();");
31+
statements.Add($"return {entity.VariableName}.{mappingMethod}(x => mapper.{entity.ElementModel.Name}To{returnDto}(x));");
32+
}
33+
34+
public bool HasProjectTo() => false;
35+
}
Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,51 @@
1-
using Intent.Metadata.Models;
2-
using Intent.Modules.Common.CSharp.Builder;
3-
using Intent.Modules.Common.CSharp.Interactions;
1+
using Intent.Modules.Common.CSharp.Builder;
42
using Intent.Utils;
5-
using System;
63
using System.Collections.Generic;
74
using System.Linq;
8-
using System.Text;
9-
using System.Threading.Tasks;
10-
using static Intent.Modules.Constants.TemplateRoles.Domain;
115

12-
namespace Intent.Modules.Application.DomainInteractions.Strategies
6+
namespace Intent.Modules.Application.DomainInteractions.Strategies;
7+
8+
public class MappingStrategyProvider
139
{
14-
public class MappingStrategyProvider
10+
private record StrategyEntry(IMappingStrategy Strategy, int Priority);
11+
12+
private readonly List<StrategyEntry> _strategies = new();
13+
14+
public static readonly MappingStrategyProvider Instance = new();
15+
16+
public void Register(IMappingStrategy strategy)
1517
{
16-
private readonly List<(IMappingStrategy Strategy, int Priority)> _strategies = new List<(IMappingStrategy, int)>();
18+
Register(strategy, 0);
19+
}
1720

18-
public static readonly MappingStrategyProvider Instance = new MappingStrategyProvider();
21+
public void Register(IMappingStrategy strategy, int priority)
22+
{
23+
_strategies.Add(new (strategy, priority));
24+
}
1925

20-
public void Register(IMappingStrategy strategy)
26+
public bool HasMappingStrategy(ICSharpClassMethodDeclaration method)
27+
{
28+
var foundStrategies = _strategies.Where(x => x.Strategy.IsMatch(method)).ToList();
29+
if (foundStrategies.Count > 1)
2130
{
22-
Register(strategy, 0);
31+
Logging.Log.Debug($"Multiple mapping strategies matched for {method}: [{string.Join(", ", foundStrategies)}]");
2332
}
2433

25-
public void Register(IMappingStrategy strategy, int priority)
26-
{
27-
_strategies.Add((strategy, priority));
28-
}
34+
return foundStrategies.Count > 0;
35+
}
2936

30-
public bool HasMappingStrategy(ICSharpClassMethodDeclaration method)
37+
public IMappingStrategy? GetMappingStrategy(ICSharpClassMethodDeclaration method)
38+
{
39+
var foundStrategies = _strategies.OrderBy(x => x.Priority).Where(x => x.Strategy.IsMatch(method)).ToList();
40+
if (foundStrategies.Count == 0)
3141
{
32-
ICSharpClassMethodDeclaration method2 = method;
33-
List<(IMappingStrategy, int)> list = _strategies.Where<(IMappingStrategy, int)>(((IMappingStrategy Strategy, int Priority) x) => x.Strategy.IsMatch(method2)).ToList();
34-
if (list.Count > 1)
35-
{
36-
Logging.Log.Debug($"Multiple mapping strategies matched for {method2}: [{string.Join(", ", list)}]");
37-
}
38-
39-
return list.Count > 0;
42+
Logging.Log.Warning($"No mapping strategy matched for {method}");
43+
return null;
4044
}
41-
42-
public IMappingStrategy? GetMappingStrategy(ICSharpClassMethodDeclaration method)
45+
if (foundStrategies.Count > 1)
4346
{
44-
ICSharpClassMethodDeclaration method2 = method;
45-
List<(IMappingStrategy, int)> list = (from x in _strategies
46-
orderby x.Priority
47-
where x.Strategy.IsMatch(method2)
48-
select x).ToList();
49-
if (list.Count == 0)
50-
{
51-
Logging.Log.Warning($"No mapping strategy matched for {method2}");
52-
return null;
53-
}
54-
55-
if (list.Count > 1)
56-
{
57-
Logging.Log.Debug($"Multiple mapping strategies found for {method2}: [{string.Join(", ", list)}]");
58-
}
59-
60-
return list[0].Item1;
47+
Logging.Log.Debug($"Multiple mapping strategies found for {method}: [{string.Join(", ", foundStrategies)}]");
6148
}
49+
return foundStrategies[0].Strategy;
6250
}
63-
}
51+
}

0 commit comments

Comments
 (0)