Refactor generator Abstractions symbol names to centralized nameof-based constants#23
Conversation
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
nameof-based constants
There was a problem hiding this comment.
Pull request overview
This PR refactors the MattSourceGenHelpers.Generators project to eliminate hard-coded Abstractions type/member string literals in GeneratesMethodExecutionRuntime.cs, replacing them with centralized nameof-based constants defined in Consts.cs. This makes the generator's references to Abstractions symbols refactor-safe and consistent with how other symbol names were already defined in Consts.
Changes:
- Added five new constants to
Consts.cs:AbstractionsAssemblyName,GenerateTypeFullName,RecordingGeneratorsFactoryTypeFullName,CurrentGeneratorPropertyName, andLastRecordPropertyName. - Removed five private
const stringdeclarations fromGeneratesMethodExecutionRuntime.cs. - Updated all usages in
GeneratesMethodExecutionRuntime.csto reference the newConstsproperties.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
MattSourceGenHelpers.Generators/Consts.cs |
Adds Abstractions assembly name, type full names, and property name constants derived from nameof |
MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs |
Removes local private const strings and replaces all usages with Consts.* references |
One issue was identified:
AbstractionsAssemblyName on line 10 of Consts.cs is defined as a direct alias of AbstractionsNamespace. While currently correct (both resolve to "MattSourceGenHelpers.Abstractions"), these are semantically distinct concepts — an assembly name and a CLR namespace. If the assembly name were ever overridden in the .csproj (via <AssemblyName>) without a corresponding namespace change (or vice versa), this constant would silently produce the wrong value and assembly lookup in ExecuteFluentGeneratorMethod would break. A dedicated, independent constant (e.g., using typeof(Generate).Assembly.GetName().Name at runtime, or a standalone literal) would be more robust.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public static class Consts | ||
| { | ||
| public const string AbstractionsNamespace = $"{nameof(MattSourceGenHelpers)}.{nameof(Abstractions)}"; | ||
| public const string AbstractionsAssemblyName = AbstractionsNamespace; |
There was a problem hiding this comment.
AbstractionsAssemblyName is defined as a simple alias of AbstractionsNamespace. While these values happen to be identical today ("MattSourceGenHelpers.Abstractions"), they represent two conceptually different things: a namespace and an assembly name. Assembly names and namespaces can diverge independently (e.g., if the project's <AssemblyName> is overridden without changing the namespace, or vice versa). A more robust definition would derive AbstractionsAssemblyName independently from an <AssemblyName> expression or a nameof-based approach that directly references the assembly identity rather than reusing the namespace constant.
| public const string AbstractionsAssemblyName = AbstractionsNamespace; | |
| public static readonly string AbstractionsAssemblyName = typeof(Generate).Assembly.GetName().Name; |
Generators currently mix
nameof-based symbol references with hard-coded Abstractions type/member strings (e.g. fully-qualified type names used in reflection). This change removes copied string literals for Abstractions symbols and standardizes them viaConsts.What changed
MattSourceGenHelpers.Generators/Consts.cs, derived fromnameof(...):Generatefull type nameRecordingGeneratorsFactoryfull type nameGenerate.CurrentGeneratorproperty nameRecordingGeneratorsFactory.LastRecordproperty nameGeneratesMethodExecutionRuntimeto consume these constants instead of local hard-coded strings.Why this matters
Consts) rather than duplicating string literals in runtime code.Example
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.