RSCG – M31.FluentAPI

RSCG – M31.FluentAPI
 
 

name M31.FluentAPI
nuget https://www.nuget.org/packages/M31.FluentAPI/
link https://www.m31coding.com/blog/fluent-api.html
author Kevin Schaal

Builder for your class. But the order counts – generates a new interface each time

 

This is how you can use M31.FluentAPI .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="M31.FluentApi" Version="1.0.0" PrivateAssets="all"/>
	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


using M31FluentAPIDemo;

Console.WriteLine("Hello, World!");
var p =CreatePerson
    //the order does matter
    .Named("Andrei","Ignat")
    .HasDOB(null);



using M31.FluentApi.Attributes;

namespace M31FluentAPIDemo;
[FluentApi]
internal class Person
{
    [FluentMember(0, "Named", 0)]
    public string FirstName { get; set; } = string.Empty;
    [FluentMember(0, "Named", 1)]
    public string? LastName { get; set; }

    [FluentMember(1, "HasDOB")]
    public DateTime? DOB { get; set; }
}


 

The code that is generated is

// <auto-generated/>
// This code was generated by the library M31.FluentAPI.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.

#nullable enable

using M31.FluentApi.Attributes;

namespace M31FluentAPIDemo;

internal class CreatePerson : CreatePerson.IHasDOB
{
    private readonly Person person;

    private CreatePerson()
    {
        person = new Person();
    }

    public static IHasDOB Named(string firstName, string? lastName)
    {
        CreatePerson createPerson = new CreatePerson();
        createPerson.person.FirstName = firstName;
        createPerson.person.LastName = lastName;
        return createPerson;
    }

    public Person HasDOB(System.DateTime? dOB)
    {
        person.DOB = dOB;
        return person;
    }

    internal interface IHasDOB
    {
        Person HasDOB(System.DateTime? dOB);
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/M31.FluentAPI