From c6d604bb03c396cdb37f7e27b788a1e8f4a714eb Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 13 Sep 2024 07:57:43 +0800 Subject: [PATCH 1/3] IsEnum --- src/CodeGeneratorPackage.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/CodeGeneratorPackage.cs b/src/CodeGeneratorPackage.cs index 6f6d748..5c60e99 100644 --- a/src/CodeGeneratorPackage.cs +++ b/src/CodeGeneratorPackage.cs @@ -38,7 +38,7 @@ public sealed class CodeGeneratorPackage : AsyncPackage public static DTE2 _dte; - protected async override System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) + protected async override Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) { await JoinableTaskFactory.SwitchToMainThreadAsync(); @@ -62,8 +62,10 @@ private void ExecuteAsync(object sender, EventArgs e) NewItemTarget infrastructure = NewItemTarget.Create(_dte, "Infrastructure"); NewItemTarget ui = NewItemTarget.Create(_dte, "Server.UI"); var includes = new string[] { "IEntity", "BaseEntity", "BaseAuditableEntity", "BaseAuditableSoftDeleteEntity", "AuditTrail", "OwnerPropertyEntity","KeyValue" }; + + var objectlist = ProjectHelpers.GetEntities(domain.Project) - .Where(x => includes.Contains(x.BaseName) && !includes.Contains(x.Name)); + .Where(x => x.IsEnum || (includes.Contains(x.BaseName) && !includes.Contains(x.Name))); var entities = objectlist.Select(x=>x.Name).Distinct().ToArray(); if (target == null && target.Project.Name == APPLICATIONPROJECT) { From d9908843652a88d81cd3ef77e974ddf223843cc1 Mon Sep 17 00:00:00 2001 From: hualin Date: Fri, 13 Sep 2024 15:26:25 +0800 Subject: [PATCH 2/3] support output enum type --- src/CodeGeneratorPackage.cs | 22 ++-- src/Models/IntellisenseParser.cs | 2 +- src/Templatemap.cs | 189 ++++++++++++++++++++++--------- 3 files changed, 146 insertions(+), 67 deletions(-) diff --git a/src/CodeGeneratorPackage.cs b/src/CodeGeneratorPackage.cs index 5c60e99..233b9d6 100644 --- a/src/CodeGeneratorPackage.cs +++ b/src/CodeGeneratorPackage.cs @@ -63,10 +63,10 @@ private void ExecuteAsync(object sender, EventArgs e) NewItemTarget ui = NewItemTarget.Create(_dte, "Server.UI"); var includes = new string[] { "IEntity", "BaseEntity", "BaseAuditableEntity", "BaseAuditableSoftDeleteEntity", "AuditTrail", "OwnerPropertyEntity","KeyValue" }; - + var testlist = ProjectHelpers.GetEntities(domain.Project); var objectlist = ProjectHelpers.GetEntities(domain.Project) .Where(x => x.IsEnum || (includes.Contains(x.BaseName) && !includes.Contains(x.Name))); - var entities = objectlist.Select(x=>x.Name).Distinct().ToArray(); + var entities = objectlist.Where(x=>x.IsEnum==false).Select(x=>x.Name).ToArray(); if (target == null && target.Project.Name == APPLICATIONPROJECT) { MessageBox.Show( @@ -139,7 +139,7 @@ private void ExecuteAsync(object sender, EventArgs e) }; foreach (var item in list) { - AddItemAsync(objectClass,item, name, target).Forget(); + AddItemAsync(objectClass,item, name, target, objectlist).Forget(); } var pages = new List() @@ -152,7 +152,7 @@ private void ExecuteAsync(object sender, EventArgs e) }; foreach (var item in pages) { - AddItemAsync(objectClass,item, name, ui).Forget(); + AddItemAsync(objectClass,item, name, ui, objectlist).Forget(); } } @@ -168,7 +168,7 @@ private void ExecuteAsync(object sender, EventArgs e) } } - private async Task AddItemAsync(IntellisenseObject classObject, string name,string itemname, NewItemTarget target) + private async Task AddItemAsync(IntellisenseObject classObject, string name,string itemname, NewItemTarget target,IEnumerable objectlist=null) { // The naming rules that apply to files created on disk also apply to virtual solution folders, // so regardless of what type of item we are creating, we need to validate the name. @@ -188,7 +188,7 @@ private async Task AddItemAsync(IntellisenseObject classObject, string name,stri } else { - await AddFileAsync(classObject,name, itemname, target); + await AddFileAsync(classObject,name, itemname, target, objectlist); } } @@ -212,7 +212,7 @@ private void ValidatePath(string path) } while (!string.IsNullOrEmpty(path)); } - private async Task AddFileAsync(IntellisenseObject classObject, string name,string itemname, NewItemTarget target) + private async Task AddFileAsync(IntellisenseObject classObject, string name,string itemname, NewItemTarget target,IEnumerable objectlist=null) { await JoinableTaskFactory.SwitchToMainThreadAsync(); FileInfo file; @@ -246,7 +246,7 @@ private async Task AddFileAsync(IntellisenseObject classObject, string name,stri project = target.Project; } - int position = await WriteFileAsync(project, classObject, file.FullName, itemname, target.Directory); + int position = await WriteFileAsync(project, classObject, file.FullName, itemname, target.Directory, objectlist); if (target.ProjectItem != null && target.ProjectItem.IsKind(Constants.vsProjectItemKindVirtualFolder)) { target.ProjectItem.ProjectItems.AddFromFile(file.FullName); @@ -279,9 +279,9 @@ private async Task AddFileAsync(IntellisenseObject classObject, string name,stri } } - private static async Task WriteFileAsync(Project project, IntellisenseObject classObject, string file,string itemname,string selectFolder) + private static async Task WriteFileAsync(Project project, IntellisenseObject classObject, string file,string itemname,string selectFolder,IEnumerable objectlist=null) { - string template = await TemplateMap.GetTemplateFilePathAsync(project, classObject,file, itemname, selectFolder); + string template = await TemplateMap.GetTemplateFilePathAsync(project, classObject,file, itemname, selectFolder, objectlist); if (!string.IsNullOrEmpty(template)) { @@ -301,7 +301,7 @@ private static async Task WriteFileAsync(Project project, IntellisenseObjec return 0; } - private static async System.Threading.Tasks.Task WriteToDiskAsync(string file, string content) + private static async Task WriteToDiskAsync(string file, string content) { using (StreamWriter writer = new StreamWriter(file, false, GetFileEncoding(file))) { diff --git a/src/Models/IntellisenseParser.cs b/src/Models/IntellisenseParser.cs index f9e7bb3..f7aae98 100644 --- a/src/Models/IntellisenseParser.cs +++ b/src/Models/IntellisenseParser.cs @@ -91,7 +91,7 @@ private static void ProcessElement(CodeElement element, List .FirstOrDefault(c => c.FullName != "System.Object"); } catch { /* Silently continue. */ } - var baseClasses = new string[] { "BaseAuditableSoftDeleteEntity", "BaseAuditableEntity", "BaseEntity", "IEntity", "ISoftDelete" }; + var baseClasses = new string[] { "BaseAuditableSoftDeleteEntity", "BaseAuditableEntity", "BaseEntity", "IEntity", "ISoftDelete", "OwnerPropertyEntity" }; if (baseClass != null && baseClasses.Contains(GetClassName(baseClass))) { ProcessClass(cc, baseClass, list, underProcess); diff --git a/src/Templatemap.cs b/src/Templatemap.cs index 7e79ddb..706218e 100644 --- a/src/Templatemap.cs +++ b/src/Templatemap.cs @@ -36,7 +36,7 @@ static TemplateMap() } - public static async Task GetTemplateFilePathAsync(Project project, IntellisenseObject classObject, string file, string itemname, string selectFolder) + public static async Task GetTemplateFilePathAsync(Project project, IntellisenseObject classObject, string file, string itemname, string selectFolder, IEnumerable objectlist = null) { var templatefolders = new string[]{ "Commands\\AcceptChanges", @@ -96,7 +96,7 @@ public static async Task GetTemplateFilePathAsync(Project project, Intel var tmpl = AdjustForSpecific(safeName, extension); templateFile = Path.Combine(Path.GetDirectoryName(tmplFile), tmpl + _defaultExt); //GetTemplate(tmpl); } - var template = await ReplaceTokensAsync(project, classObject, itemname, relative, selectRelative, templateFile); + var template = await ReplaceTokensAsync(project, classObject, itemname, relative, selectRelative, templateFile, objectlist); return NormalizeLineEndings(template); } @@ -117,7 +117,7 @@ private static void AddTemplatesFromCurrentFolder(List list, string dir) list.InsertRange(0, dynaList); } - private static async Task ReplaceTokensAsync(Project project, IntellisenseObject classObject, string name, string relative, string selectRelative, string templateFile) + private static async Task ReplaceTokensAsync(Project project, IntellisenseObject classObject, string name, string relative, string selectRelative, string templateFile, IEnumerable objectlist = null) { if (string.IsNullOrEmpty(templateFile)) { @@ -140,13 +140,13 @@ private static async Task ReplaceTokensAsync(Project project, Intellisen { var content = await reader.ReadToEndAsync(); var nameofPlural = ProjectHelpers.Pluralize(name); - var dtoFieldDefinition = createDtoFieldDefinition(classObject); - var importFuncExpression = createImportFuncExpression(classObject); + var dtoFieldDefinition = createDtoFieldDefinition(classObject, objectlist); + var importFuncExpression = createImportFuncExpression(classObject, objectlist); var templateFieldDefinition = createTemplateFieldDefinition(classObject); - var exportFuncExpression = createExportFuncExpression(classObject); + var exportFuncExpression = createExportFuncExpression(classObject, objectlist); var mudTdDefinition = createMudTdDefinition(classObject); - var mudTdHeaderDefinition = createMudTdHeaderDefinition(classObject); - var mudFormFieldDefinition = createMudFormFieldDefinition(classObject); + var mudTdHeaderDefinition = createMudTdHeaderDefinition(classObject, objectlist); + var mudFormFieldDefinition = createMudFormFieldDefinition(classObject, objectlist); var fieldAssignmentDefinition = createFieldAssignmentDefinition(classObject); return content.Replace("{rootnamespace}", _defaultNamespace) .Replace("{namespace}", ns) @@ -189,7 +189,7 @@ private static string splitCamelCase(string str) // Define the regular expression to split the CamelCase string var r = new Regex(@"(?<=[A-Z])(?=[A-Z][a-z]) | (?<=[^A-Z])(?=[A-Z]) | - (?<=[A-Za-z])(?=[^A-Za-z])", + (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); // Allows formatting with spaces for better readability // Use the regular expression to replace matches with a space @@ -209,7 +209,7 @@ private static string splitCamelCase(string str) } public const string PRIMARYKEY = "Id"; - private static string createDtoFieldDefinition(IntellisenseObject classObject) + private static string createDtoFieldDefinition(IntellisenseObject classObject, IEnumerable objectlist = null) { var output = new StringBuilder(); foreach (var property in classObject.Properties.Where(x => x.Type.IsDictionary == false)) @@ -223,15 +223,23 @@ private static string createDtoFieldDefinition(IntellisenseObject classObject) { switch (property.Type.CodeName) { - case "string" when property.Name.Equals("Name", StringComparison.OrdinalIgnoreCase): - output.Append($" public {property.Type.CodeName} {property.Name} {{get;set;}} = String.Empty; \r\n"); - break; - case "string" when !property.Name.Equals("Name", StringComparison.OrdinalIgnoreCase) && !property.Type.IsArray && !property.Type.IsDictionary: - output.Append($" public {property.Type.CodeName}? {property.Name} {{get;set;}} \r\n"); - break; - case "string" when !property.Name.Equals("Name", StringComparison.OrdinalIgnoreCase) && property.Type.IsArray: - output.Append($" public HashSet<{property.Type.CodeName}>? {property.Name} {{get;set;}} \r\n"); + case "string": + case "string?": + if (property.Type.IsArray) + { + output.Append($" public {property.Type.CodeName}[]{(property.Type.IsOptional ? "?" : "")} {property.Name} {{get;set;}} \r\n"); + } + else if (property.Type.IsDictionary) + { + output.Append($" public Dictionary<{property.Type.CodeName},{property.Type.CodeName}>{(property.Type.IsOptional ? "?" : "")} {property.Name} {{get;set;}} \r\n"); + } + else + { + + output.Append($" public {property.Type.CodeName}{(property.Name.Equals("Name") ? "" : "?")} {property.Name} {{get;set;}} \r\n"); + } break; + case "System.DateTime?": output.Append($" public DateTime? {property.Name} {{get;set;}} \r\n"); break; @@ -253,6 +261,13 @@ private static string createDtoFieldDefinition(IntellisenseObject classObject) case "System.Guid": output.Append($" public Guid {property.Name} {{get;set;}} \r\n"); break; + case "System.Guid?": + output.Append($" public Guid? {property.Name} {{get;set;}} \r\n"); + break; + case "bool?": + case "bool": + case "byte?": + case "byte": case "char?": case "char": case "float?": @@ -263,53 +278,72 @@ private static string createDtoFieldDefinition(IntellisenseObject classObject) case "int": case "double?": case "double": - output.Append($" public {property.Type.CodeName} {property.Name} {{get;set;}} \r\n"); + output.Append($" public {property.Type.CodeName}{(property.Type.IsArray ? "[]" : "")} {property.Name} {{get;set;}} \r\n"); break; default: - if (property.Type.TypeScriptName == "any") + if (objectlist != null && objectlist.Any(x => x.FullName.Equals(property.Type.CodeName))) { var complexType = property.Type.CodeName.Split('.').Last(); - if (property.Type.Shape != null && property.Type.Shape.Any()) - { - complexType = complexType + "Dto"; - } - else if (property.Type.IsArray) + var relatedObject = objectlist.First(x => x.FullName.Equals(property.Type.CodeName)); + if (relatedObject.IsEnum) { - complexType = $"List<{complexType}Dto>"; - } - output.Append($" public {complexType}{(complexType.Any(x => x == '?') ? "" : "?")} {property.Name} {{get;set;}} \r\n"); - } - else - { - if (property.Type.IsOptional) - { - output.Append($" public {property.Type.CodeName}? {property.Name} {{get;set;}} \r\n"); + output.Append($" public {complexType}? {property.Name} {{get;set;}} \r\n"); } else { - output.Append($" public {property.Type.CodeName} {property.Name} {{get;set;}} \r\n"); + complexType = complexType + "Dto"; + if (property.Type.IsArray) + { + complexType = $"List<{complexType}Dto>?"; + } + output.Append($" public {complexType} {property.Name} {{get;set;}} \r\n"); } } break; } - } } return output.ToString(); } - private static string createImportFuncExpression(IntellisenseObject classObject) + private static string createImportFuncExpression(IntellisenseObject classObject, IEnumerable objectlist = null) { var output = new StringBuilder(); - foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true)) + foreach (var property in classObject.Properties.Where(x => !x.Type.IsDictionary && !x.Type.IsArray)) { if (property.Name == PRIMARYKEY) continue; + if (property.Type.CodeName.StartsWith("bool")) { output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} =Convert.ToBoolean(row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]]) }}, \r\n"); } + else if (property.Type.CodeName.StartsWith("System.DateTime")) + { + output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} =DateTime.Parse(row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]].ToString()) }}, \r\n"); + } + else if (property.Type.CodeName.StartsWith("int")) + { + output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} =Convert.ToInt32(row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]]) }}, \r\n"); + } + else if (property.Type.CodeName.StartsWith("decimal") || property.Type.CodeName.StartsWith("float")) + { + output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} =Convert.ToDecimal(row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]]) }}, \r\n"); + } else { - output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} = row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]].ToString() }}, \r\n"); + if (property.Type.IsKnownType) + { + output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} = row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]].ToString() }}, \r\n"); + } + else + { + var relatedObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName) && x.IsEnum); + if (relatedObject != null) + { + var enumType = property.Type.CodeName.Split('.').Last(); + output.Append($"{{ _localizer[_dto.GetMemberDescription(x=>x.{property.Name})], (row, item) => item.{property.Name} = Enum.Parse<{enumType}>(row[_localizer[_dto.GetMemberDescription(x=>x.{property.Name})]].ToString()) }}, \r\n"); + } + } + } } return output.ToString(); @@ -317,24 +351,36 @@ private static string createImportFuncExpression(IntellisenseObject classObject) private static string createTemplateFieldDefinition(IntellisenseObject classObject) { var output = new StringBuilder(); - foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true)) + foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true && !x.Type.IsDictionary && !x.Type.IsArray)) { if (property.Name == PRIMARYKEY) continue; output.Append($"_localizer[_dto.GetMemberDescription(x=>x.{property.Name})], \r\n"); } return output.ToString(); } - private static string createExportFuncExpression(IntellisenseObject classObject) + private static string createExportFuncExpression(IntellisenseObject classObject, IEnumerable objectlist = null) { var output = new StringBuilder(); - foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true)) + foreach (var property in classObject.Properties.Where(x => !x.Type.IsDictionary && !x.Type.IsArray)) { - output.Append($"{{_localizer[_dto.GetMemberDescription(x=>x.{property.Name})],item => item.{property.Name}}}, \r\n"); + if (property.Type.IsKnownType) + { + output.Append($"{{_localizer[_dto.GetMemberDescription(x=>x.{property.Name})],item => item.{property.Name}}}, \r\n"); + } + else + { + var relatedObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName) && x.IsEnum); + if (relatedObject != null) + { + output.Append($"{{_localizer[_dto.GetMemberDescription(x=>x.{property.Name})],item => item.{property.Name}?.ToString()}}, \r\n"); + } + } + } return output.ToString(); } - private static string createMudTdHeaderDefinition(IntellisenseObject classObject) + private static string createMudTdHeaderDefinition(IntellisenseObject classObject, IEnumerable objectlist = null) { var output = new StringBuilder(); var defaultfieldName = new string[] { "Name", "Description" }; @@ -355,11 +401,27 @@ private static string createMudTdHeaderDefinition(IntellisenseObject classObject output.Append(" \r\n"); output.Append($"\r\n"); } - foreach (var property in classObject.Properties.Where(x => !defaultfieldName.Contains(x.Name))) + foreach (var property in classObject.Properties.Where(x => !x.Type.IsDictionary && !x.Type.IsArray && !defaultfieldName.Contains(x.Name))) { if (property.Name == PRIMARYKEY) continue; - output.Append(" "); - output.Append($" x.{property.Name}\" Title=\"@L[_currentDto.GetMemberDescription(x=>x.{property.Name})]\" />\r\n"); + if (property.Type.IsKnownType) + { + output.Append(" "); + output.Append($" x.{property.Name}\" Title=\"@L[_currentDto.GetMemberDescription(x=>x.{property.Name})]\" />\r\n"); + } + else + { + var relatedObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName) && x.IsEnum); + if (relatedObject != null) + { + output.Append(" "); + output.Append($" x.{property.Name}\" Title=\"@L[_currentDto.GetMemberDescription(x=>x.{property.Name})]\">\r\n"); + output.Append(" \r\n"); + output.Append($" \r\n"); + output.Append(" \r\n"); + output.Append($"\r\n"); + } + } } return output.ToString(); } @@ -388,7 +450,7 @@ private static string createMudTdDefinition(IntellisenseObject classObject) output.Append(" "); output.Append($"\r\n"); } - foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true && !defaultfieldName.Contains(x.Name))) + foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true && !x.Type.IsDictionary && !x.Type.IsArray && !defaultfieldName.Contains(x.Name))) { if (property.Name == PRIMARYKEY) continue; output.Append(" "); @@ -413,10 +475,10 @@ private static string createMudTdDefinition(IntellisenseObject classObject) return output.ToString(); } - private static string createMudFormFieldDefinition(IntellisenseObject classObject) + private static string createMudFormFieldDefinition(IntellisenseObject classObject, IEnumerable objectlist = null) { var output = new StringBuilder(); - foreach (var property in classObject.Properties.Where(x => x.Type.IsKnownType == true)) + foreach (var property in classObject.Properties.Where(x => !x.Type.IsDictionary && !x.Type.IsArray)) { if (property.Name == PRIMARYKEY) continue; switch (property.Type.CodeName.ToLower()) @@ -467,6 +529,7 @@ private static string createMudFormFieldDefinition(IntellisenseObject classObjec output.Append(" "); output.Append($" \r\n"); break; + case "system.datetime": case "system.datetime?": output.Append($" \r\n"); output.Append(" "); @@ -475,11 +538,27 @@ private static string createMudFormFieldDefinition(IntellisenseObject classObjec output.Append($" \r\n"); break; default: - output.Append($" \r\n"); - output.Append(" "); - output.Append($" x.{property.Name})]\" @bind-Value=\"model.{property.Name}\" For=\"@(() => model.{property.Name})\" Required=\"false\" RequiredError=\"@L[\"{splitCamelCase(property.Name).ToLower()} is required!\"]\">\r\n"); - output.Append(" "); - output.Append($" \r\n"); + if (property.Type.IsKnownType) + { + output.Append($" \r\n"); + output.Append(" "); + output.Append($" x.{property.Name})]\" @bind-Value=\"model.{property.Name}\" For=\"@(() => model.{property.Name})\" Required=\"false\" RequiredError=\"@L[\"{splitCamelCase(property.Name).ToLower()} is required!\"]\">\r\n"); + output.Append(" "); + output.Append($" \r\n"); + } + else + { + var relatedObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName) && x.IsEnum); + if (relatedObject != null) + { + var enumType = property.Type.CodeName.Split('.').Last(); + output.Append($" \r\n"); + output.Append(" "); + output.Append($" \" Label=\"@L[model.GetMemberDescription(x=>x.{property.Name})]\" @bind-Value=\"model.{property.Name}\" For=\"@(() => model.{property.Name})\" Required=\"false\" RequiredError=\"@L[\"{splitCamelCase(property.Name).ToLower()} is required!\"]\">\r\n"); + output.Append(" "); + output.Append($" \r\n"); + } + } break; } From 70cdc3ad208f028bcd8451c6a61040f6bb9af110 Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Sat, 14 Sep 2024 09:15:56 +0800 Subject: [PATCH 3/3] entityTypeBuilderConfirmation, commandValidatorRuleFor --- src/CodeGeneratorPackage.cs | 2 +- src/Templatemap.cs | 175 ++++++++++++++++-- .../Commands/AddEdit/.validator.cs.txt | 5 +- .../Commands/Create/.validator.cs.txt | 6 +- .../Commands/Update/.validator.cs.txt | 2 +- .../Configurations/.configuration.cs.txt | 2 +- 6 files changed, 166 insertions(+), 26 deletions(-) diff --git a/src/CodeGeneratorPackage.cs b/src/CodeGeneratorPackage.cs index 233b9d6..1a7c84c 100644 --- a/src/CodeGeneratorPackage.cs +++ b/src/CodeGeneratorPackage.cs @@ -108,7 +108,7 @@ private void ExecuteAsync(object sender, EventArgs e) }; foreach (var item in configurations) { - AddItemAsync(objectClass, item, name, infrastructure).Forget(); + AddItemAsync(objectClass, item, name, infrastructure, objectlist).Forget(); } var list = new List() diff --git a/src/Templatemap.cs b/src/Templatemap.cs index 706218e..f4c6716 100644 --- a/src/Templatemap.cs +++ b/src/Templatemap.cs @@ -148,20 +148,33 @@ private static async Task ReplaceTokensAsync(Project project, Intellisen var mudTdHeaderDefinition = createMudTdHeaderDefinition(classObject, objectlist); var mudFormFieldDefinition = createMudFormFieldDefinition(classObject, objectlist); var fieldAssignmentDefinition = createFieldAssignmentDefinition(classObject); - return content.Replace("{rootnamespace}", _defaultNamespace) - .Replace("{namespace}", ns) - .Replace("{selectns}", selectNs) - .Replace("{itemname}", name) - .Replace("{nameofPlural}", nameofPlural) - .Replace("{dtoFieldDefinition}", dtoFieldDefinition) - .Replace("{fieldAssignmentDefinition}", fieldAssignmentDefinition) - .Replace("{importFuncExpression}", importFuncExpression) - .Replace("{templateFieldDefinition}", templateFieldDefinition) - .Replace("{exportFuncExpression}", exportFuncExpression) - .Replace("{mudTdDefinition}", mudTdDefinition) - .Replace("{mudTdHeaderDefinition}", mudTdHeaderDefinition) - .Replace("{mudFormFieldDefinition}", mudFormFieldDefinition) - ; + var entityTypeBuilderConfirmation = createEntityTypeBuilderConfirmation(classObject, objectlist); + var commandValidatorRuleFor = createComandValidatorRuleFor(classObject, objectlist); + var replacements = new Dictionary + { + { "rootnamespace", _defaultNamespace }, + { "namespace", ns }, + { "selectns", selectNs }, + { "itemname", name }, + { "nameofPlural", nameofPlural }, + { "dtoFieldDefinition", dtoFieldDefinition }, + { "fieldAssignmentDefinition", fieldAssignmentDefinition }, + { "importFuncExpression", importFuncExpression }, + { "templateFieldDefinition", templateFieldDefinition }, + { "exportFuncExpression", exportFuncExpression }, + { "mudTdDefinition", mudTdDefinition }, + { "mudTdHeaderDefinition", mudTdHeaderDefinition }, + { "mudFormFieldDefinition", mudFormFieldDefinition }, + { "entityTypeBuilderConfirmation", entityTypeBuilderConfirmation }, + { "commandValidatorRuleFor", commandValidatorRuleFor } + }; + + string result = Regex.Replace(content, @"\{(\w+)\}", match => { + string key = match.Groups[1].Value; + return replacements.TryGetValue(key, out var value) ? value : match.Value; + }); + return result; + } } @@ -299,10 +312,24 @@ private static string createDtoFieldDefinition(IntellisenseObject classObject, I output.Append($" public {complexType} {property.Name} {{get;set;}} \r\n"); } } + else + { + if (property.Name.Equals("Tenant")) + { + output.Append($" public TenantDto? {property.Name} {{get;set;}} \r\n"); + } + } break; } } } + + + if (classObject.BaseName.Equals("OwnerPropertyEntity")) + { + output.Append($" [Description(\"Owner\")] public ApplicationUserDto? Owner {{ get; set; }} \r\n"); + output.Append($" [Description(\"Last modifier\")] public ApplicationUserDto? LastModifier {{ get; set; }} \r\n"); + } return output.ToString(); } private static string createImportFuncExpression(IntellisenseObject classObject, IEnumerable objectlist = null) @@ -577,5 +604,125 @@ private static string createFieldAssignmentDefinition(IntellisenseObject classOb } return output.ToString(); } + + private static string createEntityTypeBuilderConfirmation(IntellisenseObject classObject, IEnumerable objectlist = null) + { + var output = new StringBuilder(); + foreach (var property in classObject.Properties.Where(x => x.Name != "Id")) + { + switch (property.Type.CodeName) + { + case "string": + case "string?": + if (property.Type.IsArray) + { + output.Append($" builder.Property(e => e.{property.Name}).HasStringListConversion(); \r\n"); + } + else if (property.Type.IsDictionary) + { + output.Append($" builder.Property(u => u.{property.Name}).HasJsonConversion(); \r\n"); + } + else if (property.Name.Equals("Name")) + { + output.Append($" builder.HasIndex(x => x.{property.Name}); \r\n"); + output.Append($" builder.Property(x => x.{property.Name}).HasMaxLength(50).IsRequired(); \r\n"); + } + else + { + output.Append($" builder.Property(x => x.{property.Name}).HasMaxLength(255); \r\n"); + } + break; + default: + if (!property.Type.IsKnownType) + { + var refObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName)); + if (refObject != null) + { + var complexType = property.Type.CodeName.Split('.').Last(); + if (refObject.IsEnum) + { + output.Append($" builder.HasIndex(x => x.{property.Name}); \r\n"); + output.Append($" builder.Property(t => t.{property.Name}).HasConversion().HasMaxLength(50); \r\n"); + } + else if (!property.Type.IsArray) + { + var foreignKey = property.Name + "Id"; + if (classObject.Properties.Any(x => x.Name.Equals(foreignKey))) + { + output.Append($" builder.HasOne(x => x.{property.Name}).WithMany().HasForeignKey(x => x.{foreignKey}); \r\n"); + } + } + } + else + { + if (property.Name.Equals("Tenant") && classObject.Properties.Any(x => x.Name.Equals("TenantId"))) + { + output.Append($" builder.HasOne(x => x.{property.Name}).WithMany().HasForeignKey(x => x.TenantId); \r\n"); + output.Append($" builder.Navigation(e => e.{property.Name}).AutoInclude(); \r\n"); + } + } + + } + break; + } + } + + if (classObject.BaseName.Equals("OwnerPropertyEntity")) + { + output.Append($" builder.HasOne(x => x.Owner).WithMany().HasForeignKey(x => x.CreatedBy); \r\n"); + output.Append($" builder.HasOne(x => x.LastModifier).WithMany().HasForeignKey(x => x.LastModifiedBy); \r\n"); + output.Append($" builder.Navigation(e => e.Owner).AutoInclude(); \r\n"); + output.Append($" builder.Navigation(e => e.LastModifier).AutoInclude(); \r\n"); + } + return output.ToString(); + } + + + private static string createComandValidatorRuleFor(IntellisenseObject classObject, IEnumerable objectlist = null) + { + var output = new StringBuilder(); + foreach (var property in classObject.Properties.Where(x => x.Name != "Id")) + { + switch (property.Type.CodeName) + { + case "string": + case "string?": + if (property.Name.Equals("Name")) + { + output.Append($" RuleFor(v => v.{property.Name}).MaximumLength(50).NotEmpty(); \r\n"); + } + else if (!property.Type.IsDictionary && !property.Type.IsArray) + { + output.Append($" RuleFor(v => v.{property.Name}).MaximumLength(255); \r\n"); + } + break; + case "System.TimeSpan": + case "System.DateTime": + case "System.DateTimeOffset": + case "int": + case "decimal": + case "float": + output.Append($" RuleFor(v => v.{property.Name}).NotNull(); \r\n"); + break; + default: + if (!property.Type.IsKnownType) + { + var refObject = objectlist.FirstOrDefault(x => x.FullName.Equals(property.Type.CodeName)); + if (refObject != null) + { + var complexType = property.Type.CodeName.Split('.').Last(); + if (refObject.IsEnum) + { + output.Append($" RuleFor(v => v.{property.Name}).NotNull(); \r\n"); + } + + } + } + break; + } + + } + return output.ToString(); + } } } diff --git a/src/Templates/Commands/AddEdit/.validator.cs.txt b/src/Templates/Commands/AddEdit/.validator.cs.txt index d4e8126..2ad6d46 100644 --- a/src/Templates/Commands/AddEdit/.validator.cs.txt +++ b/src/Templates/Commands/AddEdit/.validator.cs.txt @@ -7,10 +7,7 @@ public class AddEdit{itemname}CommandValidator : AbstractValidator v.Name) - .MaximumLength(256) - .NotEmpty(); - + {commandValidatorRuleFor} } } diff --git a/src/Templates/Commands/Create/.validator.cs.txt b/src/Templates/Commands/Create/.validator.cs.txt index 89709c3..ebcd865 100644 --- a/src/Templates/Commands/Create/.validator.cs.txt +++ b/src/Templates/Commands/Create/.validator.cs.txt @@ -7,11 +7,7 @@ public class Create{itemname}CommandValidator : AbstractValidator v.Name) - .MaximumLength(256) - .NotEmpty(); - + {commandValidatorRuleFor} } } diff --git a/src/Templates/Commands/Update/.validator.cs.txt b/src/Templates/Commands/Update/.validator.cs.txt index da69274..09dd4e2 100644 --- a/src/Templates/Commands/Update/.validator.cs.txt +++ b/src/Templates/Commands/Update/.validator.cs.txt @@ -8,7 +8,7 @@ public class Update{itemname}CommandValidator : AbstractValidator v.Id).NotNull(); - RuleFor(v => v.Name).MaximumLength(256).NotEmpty(); + {commandValidatorRuleFor} } diff --git a/src/Templates/Persistence/Configurations/.configuration.cs.txt b/src/Templates/Persistence/Configurations/.configuration.cs.txt index cdfef21..8e41beb 100644 --- a/src/Templates/Persistence/Configurations/.configuration.cs.txt +++ b/src/Templates/Persistence/Configurations/.configuration.cs.txt @@ -10,7 +10,7 @@ public class {itemname}Configuration : IEntityTypeConfiguration<{itemname}> { public void Configure(EntityTypeBuilder<{itemname}> builder) { - builder.Property(t => t.Name).HasMaxLength(50).IsRequired(); + {entityTypeBuilderConfirmation} builder.Ignore(e => e.DomainEvents); } }