-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEntityConfig.cs
More file actions
186 lines (154 loc) · 7.94 KB
/
Copy pathEntityConfig.cs
File metadata and controls
186 lines (154 loc) · 7.94 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace CoreEx.CodeGen.RefData.Config;
/// <summary>
/// Provides the entity code-generation configuration.
/// </summary>
[CodeGenClass("Entity", Title = "Reference-data entity configuration.")]
[CodeGenCategory("Primary", Title = "Provides the _primary_ configuration.")]
[CodeGenCategory("API", Title = "Provides the configuration for the generated API code.")]
[CodeGenCategory("Repository", Title = "Provides the configuration for the generated repository code.")]
[CodeGenCategory("Mapping", Title = "Provides the configuration for the generated mapping code.")]
[CodeGenCategory("Exclude", Title = "Provides the configuration for code generation exclusion.")]
[CodeGenCategory("Collections", Title = "Provides the collections configuration.")]
public class EntityConfig : ConfigBase<CodeGenConfig, CodeGenConfig>
{
/// <summary>
/// Gets or sets the entity name.
/// </summary>
[JsonPropertyName("name")]
[CodeGenProperty("Primary", Title = "The reference-data entity (contract) name.", IsMandatory = true)]
public string? Name { get; set; }
/// <summary>
/// Gets or sets the pluralized entity name.
/// </summary>
[JsonPropertyName("plural")]
[CodeGenProperty("Primary", Title = "The pluralized reference-data entity (contract) name.", IsImportant = true, Description = "Defaults to `{Name}` with the last word pluralized.")]
public string? Plural { get; set; }
/// <summary>
/// Gets or sets the entity text.
/// </summary>
[JsonPropertyName("text")]
[CodeGenProperty("Primary", Title = "The reference-data entity friendly text.", Description = "Defaults to `{Name}` converted to sentence case. This is primarily used in generated code comments.")]
public string? Text { get; set; }
/// <summary>
/// Gets or sets the identifier type.
/// </summary>
[JsonPropertyName("idType")]
[CodeGenProperty("Primary", Title = "The reference-data identifier type.", IsImportant = true, Options = ["String", "Guid", "Int32", "Int64"], Description = "Defaults to root `{IdType}`.")]
public string? IdType { get; set; }
/// <summary>
/// Gets or sets the default collection sort order.
/// </summary>
[JsonPropertyName("collectionSortOrder")]
[CodeGenProperty("Primary", Title = "The collection sort order.", IsImportant = true, Options = ["Code", "Id", "Text", "SortOrder"], Description = "This is the collection sort order. Defaults to root `{CollectionSortOrder}`.")]
public string? CollectionSortOrder { get; set; }
#region API
/// <summary>
/// Gets or sets the route suffix.
/// </summary>
[JsonPropertyName("route")]
[CodeGenProperty("API", Title = "The route suffix.", IsImportant = true, Description = "Defaults to `{Plural}` and root `{RouteConvention}` configuration.")]
public string? Route { get; set; }
#endregion
#region Repository
/// <summary>
/// Gets or sets the repository implementation.
/// </summary>
[JsonPropertyName("repository")]
[CodeGenProperty("Repository", Title = "The repository implementation.", IsImportant = true, Options = ["None", "EntityFramework"], Description = "Defaults to root `{Repository}`.")]
public string? Repository { get; set; }
/// <summary>
/// Gets or sets the repository parameter name.
/// </summary>
[JsonPropertyName("repositoryName")]
[CodeGenProperty("Repository", Title = "The repository parameter name.", IsImportant = true, Description = "This is the .NET repository parameter name that should be used within the generated code. Defaults from root `{Repository}` and related configuration.")]
public string? RepositoryName { get; set; }
/// <summary>
/// Gets or sets the corresponding repository model name.
/// </summary>
[JsonPropertyName("model")]
[CodeGenProperty("Repository", Title = "The corresponding repository model name.", IsImportant = true, Description = "Defaults to `{Name}` (assumes same).")]
public string? Model { get; set; }
#endregion
#region Mapping
/// <summary>
/// Gets or sets the mapper name.
/// </summary>
[JsonPropertyName("mapper")]
[CodeGenProperty("Mapping", Title = "The mapper name.", IsImportant = true, Description = "This is the .NET mapper name used within the generated code. Defaults to root `{Name}Mapper`.")]
public string? Mapper { get; set; }
#endregion
#region Exclude
/// <summary>
/// Indicates whether to exclude the generation of the API.
/// </summary>
[JsonPropertyName("excludeApi")]
[CodeGenProperty("Exclude", Title = "Indicates whether to exclude the generation of the API.", IsImportant = true, Description = "Defaults to `false`.")]
public bool? ExcludeApi { get; set; }
/// <summary>
/// Indicates whether to exclude the generation of the mapper.
/// </summary>
[JsonPropertyName("excludeMapper")]
[CodeGenProperty("Exclude", Title = "Indicates whether to exclude the generation of the mapper.", IsImportant = true, Description = "Defaults to `false`.")]
public bool? ExcludeMapper { get; set; }
#endregion
#region Collections
/// <summary>
/// Gets the list of configured properties.
/// </summary>
[JsonPropertyName("properties")]
[CodeGenPropertyCollection("Collections", Title = "The property collection configuration.")]
public List<PropertyConfig>? Properties { get; set; }
/// <summary>
/// Gets the list of properties that are not excluded from the generated contract code.
/// </summary>
public List<PropertyConfig>? ContractProperties => Properties?.Where(p => !(p.ExcludeContract ?? false)).ToList() ?? [];
/// <summary>
/// Gets the list of properties that are not excluded from the generated mapping code.
/// </summary>
public List<PropertyConfig>? MappingProperties => Properties?.Where(p => !(p.ExcludeMapping ?? false)).ToList() ?? [];
#endregion
/// <summary>
/// Gets or sets the contract inherits base class name.
/// </summary>
public string? Inherits { get; set; }
/// <inheritdoc/>
protected override async Task PrepareAsync()
{
Text = DefaultWhereNull(Text, () => OnRamp.Utility.StringConverter.ToSentenceCase(Name!));
Model = DefaultWhereNull(Model, () => Name);
IdType = DefaultWhereNull(IdType, () => Root?.IdType);
CollectionSortOrder = DefaultWhereNull(CollectionSortOrder, () => Root!.CollectionSortOrder);
Repository = DefaultWhereNull(Repository, () => Root!.Repository);
IdType = DefaultWhereNull(IdType, () => Root?.IdType);
Mapper = DefaultWhereNull(Mapper, () => $"{Name}Mapper");
ExcludeMapper = DefaultWhereNull(ExcludeMapper, () => false);
Plural = DefaultWhereNull(Plural, () =>
{
// Best guess by pluralizing the last word of the name.
var words = OnRamp.Utility.StringConverter.ToSentenceCase(Name!)!.Split(' ').ToList();
words[^1] = OnRamp.Utility.StringConverter.ToPlural(words[^1]);
return string.Concat(words);
});
RepositoryName = DefaultWhereNull(RepositoryName, () => Repository switch
{
"EntityFramework" => Root!.EntityFrameworkRepositoryName,
_ => "??"
});
Route = DefaultWhereNull(Route, () => Root!.RouteConvention switch
{
"KebabCase" => OnRamp.Utility.StringConverter.ToKebabCase(Plural!),
"SnakeCase" => OnRamp.Utility.StringConverter.ToSnakeCase(Plural!),
"CamelCase" => OnRamp.Utility.StringConverter.ToCamelCase(Plural!),
_ => Plural!.ToLower(),
});
Inherits = IdType switch
{
"Int32" => $"ReferenceData<int, {Name}>",
"Int64" => $"ReferenceData<long, {Name}>",
"Guid" => $"ReferenceData<Guid, {Name}>",
_ => $"ReferenceData<{Name}>"
};
// Load the properties configuration.
Properties = await PrepareCollectionAsync(Properties).ConfigureAwait(false);
}
}