Refactor fluent generator API to explicit Method().WithParameter().WithReturnType() chain#18
Conversation
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Method().WithParameter().WithReturnType() chain
There was a problem hiding this comment.
Pull request overview
This PR refactors the fluent generator entrypoint from ambiguous Generate.MethodImplementation<...>() overloads to an explicit builder chain (Generate.Method().WithParameter<T>().WithReturnType<T>()) to make parameter vs return type intent clear, and migrates in-repo examples/tests accordingly.
Changes:
- Replaced
Generate.MethodImplementation*overloads with a builder-based fluent entrypoint (Generate.Method()+WithParameter/WithReturnType). - Migrated fluent usage in examples to the new chain.
- Updated tests to the new chain and added mapper-like coverage (
int -> string) validating both runtime output and generated code shape.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| MattSourceGenHelpers.Abstractions/Generate.cs | Removes old entrypoints and adds the new method builder chain routing to IGeneratorsFactory.CreateImplementation(...). |
| MattSourceGenHelpers.Examples/PiExampleFluent.cs | Migrates Pi fluent example to Method().WithParameter<int>().WithReturnType<int>(). |
| MattSourceGenHelpers.Examples/ColorsClassFluent.cs | Migrates Colors fluent example to Method().WithReturnType<string>(). |
| MattSourceGenHelpers.Tests/PiExampleFluentTests.cs | Updates fluent usage to the new chain and adds mapper-like tests for int -> string generation/output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public class MethodBuilder : IMethodBuilder | ||
| { | ||
| private readonly IGeneratorsFactory _generatorsFactory; | ||
|
|
||
| public MethodBuilder(IGeneratorsFactory generatorsFactory) | ||
| { | ||
| _generatorsFactory = generatorsFactory; | ||
| } | ||
|
|
||
| public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(_generatorsFactory); | ||
|
|
||
| public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TReturnType>(); | ||
| } | ||
|
|
||
| public class MethodBuilder<TArg1> : IMethodBuilder<TArg1> | ||
| { | ||
| private readonly IGeneratorsFactory _generatorsFactory; | ||
|
|
||
| public MethodBuilder(IGeneratorsFactory generatorsFactory) | ||
| { | ||
| _generatorsFactory = generatorsFactory; | ||
| } | ||
|
|
||
| public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TArg1, TReturnType>(); | ||
| } |
There was a problem hiding this comment.
MethodBuilder / MethodBuilder<TArg1> are only constructed internally via Generate.Method() and are not referenced elsewhere in the repo. Consider making these concrete builder types internal (and potentially sealed) to avoid expanding the public API surface unnecessarily; consumers can still program against the public IMethodBuilder interfaces returned by Generate.Method().
| public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator); | ||
| } | ||
|
|
||
| public interface IMethodBuilder | ||
| { | ||
| IMethodBuilder<TArg1> WithParameter<TArg1>(); | ||
| IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>(); | ||
| } |
There was a problem hiding this comment.
The Generate.Method().WithReturnType<TReturnType>() (no-parameter) path is part of the new fluent entrypoint, but current tests only exercise the .WithParameter<TArg1>().WithReturnType<TReturnType>() chain. Adding at least one test that uses the parameterless chain (similar to ColorsClassFluent) would help catch regressions in this new API surface.
This PR replaces the ambiguous
Generate.MethodImplementation<...>()entrypoint with an explicit fluent chain that makes parameter vs return type intent clear. It also updates all in-repo fluent examples/tests and removes the old API surface (no backward-compatibility shim, per issue scope).Fluent API surface refactor (Abstractions)
Generate.MethodImplementation(),Generate.MethodImplementation<TReturnType>(), andGenerate.MethodImplementation<TArg1, TReturnType>().Generate.Method()WithParameter<TArg1>()WithReturnType<TReturnType>()IGeneratorsFactory.CreateImplementation(...)methods without changing generator internals.Usage migration in examples
PiExampleFluentto:Generate.Method().WithParameter<int>().WithReturnType<int>()ColorsClassFluentto:Generate.Method().WithReturnType<string>()Usage migration + coverage in tests
int -> string) and validating generated switch output shape.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.