M31.FluentAPI icon indicating copy to clipboard operation
M31.FluentAPI copied to clipboard

Feature: Allow inheritance

Open vzam opened this issue 1 year ago • 1 comments

I currently have a use case where I created an abstract builder which receives a bunch of type parameters and the idea is that it would be possible to inherit from that builder and specify some of those type parameters. This would allow to have a generic implementation of a builder and a few ones which are very specific to their use case and project:

// no FluentApi
public abstract class GenericBuilder<T>
{
  [FluentMember]
  public T Content { get; set; }
}

[FluentApi]
public class ConcreteBuilder : GenericBuilder<int>

var builder = CreateConcreteBuilder.WithContent(1);

vzam avatar Jul 09 '24 14:07 vzam

Thank you for your request! Inheritance is the next feature I would like to look into.

Best regards, Kevin

m31coding avatar Jul 21 '24 07:07 m31coding

Hi @m31coding

What is the best practice to deal with the situation when the inherited type has less parameters in the constructor and provides some predefined values to the base type?

Simplified example:

[FluentApi]
public abstract record Creature(
    [property: FluentMember(0)] string PlanetName,
    [property: FluentMember(1)] string FirstName,
    [property: FluentMember(2)] string LastName);

[FluentApi]
public sealed record Martian(
    string FirstName,
    string LastName)
    : Creature(
        "Mars",
        FirstName,
        LastName);

the following doesn't work:

CreateMartian
    .WithFirstName("Hans")
    .WithLastName("Martian");

In this simple example I could add FluentSkippable to the PlanetName argument - but this get's kinda complicated if you have lots of such parameters with multiple types inheriting from the same base class. You might end up adding the skippable attribute to all parameters...

Sam13 avatar Oct 06 '24 04:10 Sam13

Hi @Sam13,

That is a very good question and I currently don't have a good answer to this use case. Constructors are not taken into account by the Fluent API (they are merely used to instantiate the object with default values). Hence, the gist of the question is 'How to provide predefined values to the base class'? Unfortunately, I assume new syntax / a new feature is required to make this work.

I'll look into this further, and in the meantime, if you have any ideas or suggestions, please let me know!

Best regards, Kevin

m31coding avatar Oct 12 '24 08:10 m31coding