RSCG – Fluentify
Generate fluent builder
This is how you can use Fluentify .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fluentify" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
The code that you will use is
using Builder;
var pOld = new Person();
pOld= pOld.WithFirstName("Andrei").WithLastName("Ignat").WithMiddleName("G");
System.Console.WriteLine(pOld.FullName());
namespace Builder;
[Fluentify.Fluentify]
public partial class Person
{
public string FirstName { get; init; }
public string? MiddleName { get; init; }
public string LastName { get; init; }
public string FullName()
{
return FirstName + " " + MiddleName + " "+LastName;
}
}
The code that is generated is
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable enable
#endif
#pragma warning disable CS8625
namespace Builder
{
using System;
using System.Collections.Generic;
using System.Linq;
using Fluentify.Internal;
public static partial class PersonExtensions
{
public static global::Builder.Person WithFirstName(
this global::Builder.Person subject,
string value)
{
subject.ThrowIfNull("subject");
return new global::Builder.Person
{
FirstName = value,
MiddleName = subject.MiddleName,
LastName = subject.LastName,
};
}
}
}
#pragma warning restore CS8625
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable enable
#endif
#pragma warning disable CS8625
namespace Builder
{
using System;
using System.Collections.Generic;
using System.Linq;
using Fluentify.Internal;
public static partial class PersonExtensions
{
public static global::Builder.Person WithLastName(
this global::Builder.Person subject,
string value)
{
subject.ThrowIfNull("subject");
return new global::Builder.Person
{
FirstName = subject.FirstName,
MiddleName = subject.MiddleName,
LastName = value,
};
}
}
}
#pragma warning restore CS8625
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable enable
#endif
#pragma warning disable CS8625
namespace Builder
{
using System;
using System.Collections.Generic;
using System.Linq;
using Fluentify.Internal;
public static partial class PersonExtensions
{
public static global::Builder.Person WithMiddleName(
this global::Builder.Person subject,
string? value)
{
subject.ThrowIfNull("subject");
return new global::Builder.Person
{
FirstName = subject.FirstName,
MiddleName = value,
LastName = subject.LastName,
};
}
}
}
#pragma warning restore CS8625
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
namespace Fluentify
{
using System;
using System.Diagnostics.CodeAnalysis;
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal sealed class DescriptorAttribute
: Attribute
{
public DescriptorAttribute(string value)
{
Value = value;
}
public string Value { get; }
}
}
namespace Fluentify
{
using System;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class FluentifyAttribute
: Attribute
{
}
}
namespace Fluentify
{
using System;
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
internal sealed class IgnoreAttribute
: Attribute
{
}
}
namespace Fluentify.Internal
{
using System;
internal static class Extensions
{
public static void ThrowIfNull(this object subject, string paramName)
{
if (subject == null)
{
throw new ArgumentNullException(paramName);
}
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Fluentify