m31coding / M31.FluentAPI

Generate fluent builders for your C# classes with ease.
MIT License
94 stars 4 forks source link

Allow skipping steps #4

Closed vzam closed 4 months ago

vzam commented 6 months ago

As I understand it, the options to every step have to be provided and you cannot skip steps in between for a single path:

[FluentApi]
public class Api {
  [FluentMethod(0)]
  public void UseOneThing();

  [FluentMethod(0)]
  public void UseOtherThing();

  [FluentMethod(1)]
  public void ConfigureOneThing();

  [FluentMethod(1)]
  public void ConfigureOneThingTheOtherWay();

  [FluentMethod(2)]
  public void Continue();
}

In this example I would like to skip step 1 entirely after having selected UseOtherThing() at step 0, maybe with a new attribute [FluentSkipTo(2)] or [FluentContinueWith(2)], also it might be considered to have [FluentBreak] to finalize the object if the last steps are no longer relevant for the chosen configuration. This can be generated as follows:

public interface IStep0
{
  IStep1 UseOneThing();
  IStep2 UseOtherThing(); // skip IStep1
}

public interface IStep1
{
  IStep2 ConfigureOneThing();
  IStep2 ConfigureOneThing();
}

public interface IStep2
{
  BuiltClass Continue();
}

I think this is a fairy common pattern and would benefit the library.

m31coding commented 5 months ago

That's an excellent idea! I also very much like your naming for the proposed attributes. Interestingly, FluentContinueWith also allows for loops and optional steps. An API like this

int hashCode = CreateHashCode
    .Add(42).Add(3.14).AddSequence(new[] { 1, 2, 3 }).Add("Hello world").Value();

could be realized by specifying step 0 for all methods and applying [FluentContinueWith(0)] to all methods except the Value method.

I plan to implement this soon. Thanks!