/
CommandFactoryInterfaceGeneratorProcessor.cs
88 lines (74 loc) · 3.78 KB
/
CommandFactoryInterfaceGeneratorProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2020 DHGMS Solutions and Contributors. All rights reserved.
// DHGMS Solutions and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Dhgms.Nucleotide.Generators.Features.Cqrs
{
/// <summary>
/// Generator for Command Factory Interface
/// </summary>
public sealed class CommandFactoryInterfaceGeneratorProcessor : BaseInterfaceLevelCodeGeneratorProcessor<IEntityGenerationModel>
{
protected override string GetClassSuffix()
{
return "CommandFactory";
}
protected override string[] GetClassPrefixes() => null;
protected override string[] GetInterfaceSummary(IEntityGenerationModel entityDeclaration)
{
return new []
{
$"Command factory for {entityDeclaration.ClassName}"
};
}
protected override PropertyDeclarationSyntax[] GetPropertyDeclarations(
IEntityGenerationModel entityGenerationModel, string prefix)
{
return Array.Empty<PropertyDeclarationSyntax>();
}
protected override MethodDeclarationSyntax[] GetMethodDeclarations(string className, string prefix)
{
var result = new List<MethodDeclarationSyntax>
{
GetAddMethodDeclaration(className),
GetDeleteMethodDeclaration(className),
GetUpdateMethodDeclaration(className),
};
return result.ToArray();
}
protected override string[] GetBaseInterfaces(IEntityGenerationModel entityGenerationModel, string prefix)
{
var className = entityGenerationModel.ClassName;
return new []
{
$"global::Whipstaff.Core.IAuditableCommandFactory<Commands.IAdd{className}Command, RequestDtos.Add{className}RequestDto, ResponseDtos.Add{className}ResponseDto, Commands.IDelete{className}Command, ResponseDtos.Delete{className}ResponseDto, Commands.IUpdate{className}Command, RequestDtos.Update{className}RequestDto, ResponseDtos.Update{className}ResponseDto>"
};
}
private MethodDeclarationSyntax GetAddMethodDeclaration(string entityName)
{
var methodName = "GetAddCommandAsync";
var returnType = SyntaxFactory.ParseTypeName($"System.Threading.Tasks.Task<Commands.IAdd{entityName}Command>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
return declaration;
}
private MethodDeclarationSyntax GetDeleteMethodDeclaration(string entityName)
{
var methodName = "GetDeleteCommandAsync";
var returnType = SyntaxFactory.ParseTypeName($"System.Threading.Tasks.Task<Commands.IDelete{entityName}Command>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
return declaration;
}
private MethodDeclarationSyntax GetUpdateMethodDeclaration(string entityName)
{
var methodName = "GetUpdateCommandAsync";
var returnType = SyntaxFactory.ParseTypeName($"System.Threading.Tasks.Task<Commands.IUpdate{entityName}Command>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
return declaration;
}
}
}