Skip to content

Commit

Permalink
Adds new option to code-generation to allow for setting the classes t…
Browse files Browse the repository at this point in the history
…hat are generated for Stored Procedures and Functions as internal.
  • Loading branch information
jwyza-pi committed Jan 6, 2025
1 parent 7a6c91d commit 853a016
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 28 deletions.
5 changes: 5 additions & 0 deletions samples/efcpt-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
"type": "boolean",
"title": "Use stored procedure, stored procedure result and function names from the database",
"default": true
},
"use-internal-access-modifiers-for-sprocs-and-functions": {
"type": "boolean",
"title": "When generating the stored procedure classes and helpers, set them to internal instead of public.",
"default": false
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions samples/efcpt-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@
"examples": [
false
]
},
"use-internal-access-modifiers-for-sprocs-and-functions": {
"type": "boolean",
"title": "When generating the stored procedure classes and helpers, set them to internal instead of public.",
"default": false,
"examples": [
true
]
}
},
"examples": [{
Expand Down
4 changes: 2 additions & 2 deletions src/Core/RevEng.Core.80/DbContextExtensionsSqlQuery
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using System.Threading.Tasks;

namespace #NAMESPACE#
{
public static class DbContextExtensions
#ACCESSMODIFIER# static class DbContextExtensions
{
public static async Task<List<T>> SqlQueryAsync<T>(this DbContext db, string sql, object[] parameters = null, CancellationToken cancellationToken = default)
where T : class
Expand All @@ -35,7 +35,7 @@ namespace #NAMESPACE#
}
}

public class OutputParameter<TValue>
#ACCESSMODIFIER# class OutputParameter<TValue>
{
private bool _valueSet = false;

Expand Down
20 changes: 15 additions & 5 deletions src/Core/RevEng.Core.80/ReverseEngineerRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,36 @@ private static void ValidateOptions(ReverseEngineerCommandOptions options, List<
{
if (options.UseDatabaseNames && options.CustomReplacers?.Count > 0)
{
warnings.Add($"'use-database-names' / 'UseDatabaseNames' has been set to true, but a '{Constants.RenamingFileName}' file was also found. This prevents '{Constants.RenamingFileName}' from functioning.");
warnings.Add(
$"'use-database-names' / 'UseDatabaseNames' has been set to true, but a '{Constants.RenamingFileName}' file was also found. This prevents '{Constants.RenamingFileName}' from functioning.");
}

if (options.UseT4 && options.UseT4Split)
{
warnings.Add("Both UseT4 and UseT4Split are set to true. Only one of these should be used, UseT4Split will be used.");
warnings.Add(
"Both UseT4 and UseT4Split are set to true. Only one of these should be used, UseT4Split will be used.");
options.UseT4 = false;
}

if (options.UseDbContextSplitting)
{
warnings.Add("UseDbContextSplitting (preview) is obsolete, please switch to the T4 split DbContext template.");
warnings.Add(
"UseDbContextSplitting (preview) is obsolete, please switch to the T4 split DbContext template.");
}

if (options.UseT4Split && options.UseDbContextSplitting)
{
warnings.Add("Both UseDbContextSplitting (preview) and UseT4Split are set to true. Only one of these should be used, UseT4Split will be used.");
warnings.Add(
"Both UseDbContextSplitting (preview) and UseT4Split are set to true. Only one of these should be used, UseT4Split will be used.");
options.UseDbContextSplitting = false;
}
}

if (options.UseInternalAccessModifiersForSprocsAndFunctions && !(options.UseT4 || options.UseT4Split))
{
warnings.Add("UseInternalAccessModifiersForSprocsAndFunctions is set to true, but UseT4 or UseT4Split are not true. This will result in conflicting access modifiers for the partial DBContext class. This option is intended for when the T4 templates have been used and are setting the DBContext class to internal rather than public. UseInternalAccessModifiersForSprocsAndFunctions will be reset to false to ensure a valid DBContext is generated.");
options.UseInternalAccessModifiersForSprocsAndFunctions = false;
}
}

private static SavedModelFiles CreateCleanupPaths(SavedModelFiles procedurePaths, SavedModelFiles functionPaths, SavedModelFiles filePaths)
{
Expand Down
8 changes: 6 additions & 2 deletions src/Core/RevEng.Core.80/ReverseEngineerScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public SavedModelFiles GenerateFunctions(
UseAsyncCalls = options.UseAsyncCalls,
UsePascalIdentifiers = !options.UseDatabaseNamesForRoutines,
UseDecimalDataAnnotation = options.UseDecimalDataAnnotation,
UseInternalAccessModifier = options.UseInternalAccessModifiersForSprocsAndFunctions,
};

var functionScaffoldedModel = functionModelScaffolder.ScaffoldModel(functionModel, functionOptions, schemas, ref errors);
Expand All @@ -156,7 +157,8 @@ public SavedModelFiles GenerateFunctions(
functionScaffoldedModel,
Path.GetFullPath(Path.Combine(options.ProjectPath, options.OutputPath ?? string.Empty)),
contextNamespace,
options.UseAsyncCalls);
options.UseAsyncCalls,
functionOptions.UseInternalAccessModifier);
}
}

Expand Down Expand Up @@ -212,6 +214,7 @@ public SavedModelFiles GenerateStoredProcedures(
UseSchemaNamespaces = options.UseSchemaNamespaces,
UseDecimalDataAnnotation = options.UseDecimalDataAnnotation,
UsePascalIdentifiers = !options.UseDatabaseNamesForRoutines,
UseInternalAccessModifier = options.UseInternalAccessModifiersForSprocsAndFunctions,
};

var procedureScaffoldedModel = procedureScaffolder.ScaffoldModel(procedureModel, procedureOptions, schemas, ref errors);
Expand All @@ -222,7 +225,8 @@ public SavedModelFiles GenerateStoredProcedures(
procedureScaffoldedModel,
Path.GetFullPath(Path.Combine(options.ProjectPath, options.OutputPath ?? string.Empty)),
contextNamespace,
options.UseAsyncCalls);
options.UseAsyncCalls,
procedureOptions.UseInternalAccessModifier);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected FunctionScaffolder([System.Diagnostics.CodeAnalysis.NotNull] ICSharpHe

public string FileNameSuffix { get; set; }

public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
return ScaffoldHelper.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace RevEng.Core.Routines.Functions
{
public class NotImplementedFunctionScaffolder : IFunctionScaffolder
{
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.EntityFrameworkCore.Design;
Expand All @@ -27,6 +26,8 @@ protected override string WriteDbContext(ModuleScaffolderOptions scaffolderOptio

ArgumentNullException.ThrowIfNull(model);

string accessModifier = scaffolderOptions.UseInternalAccessModifier ? "internal" : "public";

Sb = new IndentedStringBuilder();

Sb.AppendLine(PathHelper.Header);
Expand All @@ -53,7 +54,7 @@ protected override string WriteDbContext(ModuleScaffolderOptions scaffolderOptio

using (Sb.Indent())
{
Sb.AppendLine($"public partial class {scaffolderOptions.ContextName}");
Sb.AppendLine($"{accessModifier} partial class {scaffolderOptions.ContextName}");

Sb.AppendLine("{");

Expand Down
2 changes: 1 addition & 1 deletion src/Core/RevEng.Core.80/Routines/IRoutineScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RevEng.Core.Routines
{
public interface IRoutineScaffolder
{
SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls);
SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier);
ScaffoldedModel ScaffoldModel(RoutineModel model, ModuleScaffolderOptions scaffolderOptions, List<string> schemas, ref List<string> errors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace RevEng.Core.Routines.Procedures
{
public class NotImplementedProcedureScaffolder : IProcedureScaffolder
{
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Scaffolding;
Expand All @@ -24,17 +22,18 @@ public PostgresStoredProcedureScaffolder([NotNull] ICSharpHelper code, IClrTypeM
ProviderUsing = "using Npgsql";
}

public new SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public new SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
ArgumentNullException.ThrowIfNull(scaffoldedModel);

var files = base.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls);
var files = base.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls, useInternalAccessModifier);
var accessModifier = useInternalAccessModifier ? "internal" : "public";

var contextDir = Path.GetDirectoryName(Path.Combine(outputDir, scaffoldedModel.ContextFile.Path));
var dbContextExtensionsText = ScaffoldHelper.GetDbContextExtensionsText(useAsyncCalls);
var dbContextExtensionsName = useAsyncCalls ? "DbContextExtensions.cs" : "DbContextExtensions.Sync.cs";
var dbContextExtensionsPath = Path.Combine(contextDir ?? string.Empty, dbContextExtensionsName);
File.WriteAllText(dbContextExtensionsPath, dbContextExtensionsText.Replace("#NAMESPACE#", nameSpaceValue, StringComparison.OrdinalIgnoreCase), Encoding.UTF8);
File.WriteAllText(dbContextExtensionsPath, dbContextExtensionsText.Replace("#NAMESPACE#", nameSpaceValue, StringComparison.OrdinalIgnoreCase).Replace("#ACCESSMODIFIER#", accessModifier, StringComparison.OrdinalIgnoreCase), Encoding.UTF8);
files.AdditionalFiles.Add(dbContextExtensionsPath);

return files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public abstract class ProcedureScaffolder : IRoutineScaffolder
protected ProcedureScaffolder([System.Diagnostics.CodeAnalysis.NotNull] ICSharpHelper code, IClrTypeMapper typeMapper)
{
ArgumentNullException.ThrowIfNull(code);

Code = code;
this.typeMapper = typeMapper;
scaffolder = new Scaffolder(code, typeMapper);
Expand All @@ -35,7 +34,7 @@ protected ProcedureScaffolder([System.Diagnostics.CodeAnalysis.NotNull] ICSharpH

public string ProviderUsing { get; set; }

public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
return ScaffoldHelper.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls);
}
Expand Down Expand Up @@ -175,6 +174,7 @@ private string WriteDbContextInterface(ModuleScaffolderOptions scaffolderOptions
ArgumentNullException.ThrowIfNull(scaffolderOptions);

ArgumentNullException.ThrowIfNull(model);
string accessModifier = scaffolderOptions.UseInternalAccessModifier ? "internal" : "public";

Sb = new IndentedStringBuilder();

Expand All @@ -194,7 +194,7 @@ private string WriteDbContextInterface(ModuleScaffolderOptions scaffolderOptions

using (Sb.Indent())
{
Sb.AppendLine($"public partial interface I{scaffolderOptions.ContextName}{FileNameSuffix}");
Sb.AppendLine($"{accessModifier} partial interface I{scaffolderOptions.ContextName}{FileNameSuffix}");
Sb.AppendLine("{");
using (Sb.Indent())
{
Expand All @@ -219,6 +219,8 @@ private string WriteDbContext(ModuleScaffolderOptions scaffolderOptions, Routine

ArgumentNullException.ThrowIfNull(model);

string accessModifier = scaffolderOptions.UseInternalAccessModifier ? "internal" : "public";

Sb = new IndentedStringBuilder();

Sb.AppendLine(PathHelper.Header);
Expand All @@ -237,7 +239,7 @@ private string WriteDbContext(ModuleScaffolderOptions scaffolderOptions, Routine

using (Sb.Indent())
{
Sb.AppendLine($"public partial class {scaffolderOptions.ContextName}");
Sb.AppendLine($"{accessModifier} partial class {scaffolderOptions.ContextName}");
Sb.AppendLine("{");
using (Sb.Indent())
{
Expand Down Expand Up @@ -281,7 +283,7 @@ private string WriteDbContext(ModuleScaffolderOptions scaffolderOptions, Routine
Sb.AppendLine("}");
Sb.AppendLine();

Sb.AppendLine($"public partial class {scaffolderOptions.ContextName}{FileNameSuffix} : I{scaffolderOptions.ContextName}{FileNameSuffix}");
Sb.AppendLine($"{accessModifier} partial class {scaffolderOptions.ContextName}{FileNameSuffix} : I{scaffolderOptions.ContextName}{FileNameSuffix}");
Sb.AppendLine("{");

using (Sb.Indent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ public SqlServerStoredProcedureScaffolder([NotNull] ICSharpHelper code, IClrType
ProviderUsing = "using Microsoft.Data.SqlClient";
}

public new SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls)
public new SavedModelFiles Save(ScaffoldedModel scaffoldedModel, string outputDir, string nameSpaceValue, bool useAsyncCalls, bool useInternalAccessModifier)
{
ArgumentNullException.ThrowIfNull(scaffoldedModel);

var files = base.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls);
var files = base.Save(scaffoldedModel, outputDir, nameSpaceValue, useAsyncCalls, useInternalAccessModifier);
var accessModifier = useInternalAccessModifier ? "internal" : "public";

var contextDir = Path.GetDirectoryName(Path.Combine(outputDir, scaffoldedModel.ContextFile.Path));
var dbContextExtensionsText = ScaffoldHelper.GetDbContextExtensionsText(useAsyncCalls);
var dbContextExtensionsName = useAsyncCalls ? "DbContextExtensions.cs" : "DbContextExtensions.Sync.cs";
var dbContextExtensionsPath = Path.Combine(contextDir ?? string.Empty, dbContextExtensionsName);
File.WriteAllText(dbContextExtensionsPath, dbContextExtensionsText.Replace("#NAMESPACE#", nameSpaceValue, StringComparison.OrdinalIgnoreCase), Encoding.UTF8);
File.WriteAllText(dbContextExtensionsPath, dbContextExtensionsText.Replace("#NAMESPACE#", nameSpaceValue, StringComparison.OrdinalIgnoreCase).Replace("#ACCESSMODIFIER#", accessModifier, StringComparison.OrdinalIgnoreCase), Encoding.UTF8);
files.AdditionalFiles.Add(dbContextExtensionsPath);

return files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class ModuleScaffolderOptions
public virtual bool UseSchemaNamespaces { get; set; }
public virtual bool UseDecimalDataAnnotation { get; set; }
public virtual bool UsePascalIdentifiers { get; set; }
public virtual bool UseInternalAccessModifier { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/GUI/RevEng.Shared/Cli/CliConfigMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static ReverseEngineerCommandOptions ToCommandOptions(
UseDecimalDataAnnotation = config.CodeGeneration.UseDecimalDataAnnotation,
UsePrefixNavigationNaming = config.CodeGeneration.UsePrefixNavigationNaming,
UseDatabaseNamesForRoutines = config.CodeGeneration.UseDatabaseNamesForRoutines,
UseInternalAccessModifiersForSprocsAndFunctions = config.CodeGeneration.UseInternalAccessModifiersForSprocsAndFunctions,

// Not supported:
UseHandleBars = false,
Expand Down Expand Up @@ -184,6 +185,7 @@ public static ReverseEngineerOptions ToOptions(this CliConfig config, string pro
ProjectRootNamespace = names.RootNamespace,
UseDecimalDataAnnotationForSprocResult = config.CodeGeneration.UseDecimalDataAnnotation,
UsePrefixNavigationNaming = config.CodeGeneration.UsePrefixNavigationNaming,
UseDatabaseNamesForRoutines = config.CodeGeneration.UseDatabaseNamesForRoutines,

// HandleBars templates are not supported:
UseHandleBars = false,
Expand Down
4 changes: 4 additions & 0 deletions src/GUI/RevEng.Shared/Cli/Configuration/CodeGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,9 @@ public class CodeGeneration
[JsonPropertyOrder(210)]
[JsonPropertyName("use-t4-split")]
public bool UseT4Split { get; set; }

[JsonPropertyOrder(220)]
[JsonPropertyName("use-internal-access-modifiers-for-sprocs-and-functions")]
public bool UseInternalAccessModifiersForSprocsAndFunctions { get; set; }
}
}
1 change: 1 addition & 0 deletions src/GUI/RevEng.Shared/ReverseEngineerCommandOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ public class ReverseEngineerCommandOptions
public bool UseDateOnlyTimeOnly { get; set; }
public bool UsePrefixNavigationNaming { get; set; }
public bool UseDatabaseNamesForRoutines { get; set; }
public bool UseInternalAccessModifiersForSprocsAndFunctions { get; set; }
}
}
1 change: 1 addition & 0 deletions src/GUI/RevEng.Shared/ReverseEngineerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ public class ReverseEngineerOptions
public bool UsePrefixNavigationNaming { get; set; }
public bool UseAsyncStoredProcedureCalls { get; set; } = true;
public bool UseDatabaseNamesForRoutines { get; set; } = true;
public bool UseInternalAccessModifiersForSprocsAndFunctions { get; set; }
}
}

0 comments on commit 853a016

Please sign in to comment.