RSCG – Hsu.Sg.FluentMember
RSCG – Hsu.Sg.FluentMember
name | Hsu.Sg.FluentMember |
nuget | https://www.nuget.org/packages/Hsu.Sg.FluentMember/ |
link | https://github.com/hsu-net/source-generators |
author | Net Hsu |
Adding builder pattern to classes
This is how you can use Hsu.Sg.FluentMember .
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="Hsu.Sg.FluentMember" Version="2024.101.8-rc175707"> <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; [Hsu.Sg.FluentMember.FluentMember] 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
// <auto-generated/> using System; namespace Hsu.Sg.FluentMember { /// <summary> /// The flag to generate member set method. /// </summary> [AttributeUsage( AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = false, Inherited = false)] internal sealed class FluentMemberAttribute : Attribute { /// <summary> /// The public member are generated. /// </summary> public bool Public { get; set; } = true; /// <summary> /// The internal member are generated. /// </summary> public bool Internal { get; set; } /// <summary> /// The private member are generated. /// </summary> public bool Private { get; set; } /// <summary> /// Only [FluentMemberGen] member are generated. /// </summary> public bool Only { get; set; } /// <summary> /// The prefix of member name. /// </summary> /// <remarks>default is `With`</remarks> public string Prefix { get; set; } = string.Empty; } [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, AllowMultiple = false, Inherited = false)] internal sealed class FluentMemberGenAttribute : Attribute { /// <summary> /// Ignore member. /// </summary> public bool Ignore { get; set; } /// <summary> /// The specific name of member. /// </summary> public string Identifier { get; set; } = string.Empty; /// <summary> /// The prefix of member name. /// </summary> /// <remarks>default is `With`</remarks> public string Prefix { get; set; } = string.Empty; /// <summary> /// The modifier of member /// </summary> /// <remarks>default is <see cref="Accessibility.Inherit"/></remarks> public Accessibility Modifier { get; set; } = Accessibility.Inherit; } /// <summary> /// The accessibility for fluent member set method. /// </summary> //[System.DefaultValue(Inherit)] internal enum Accessibility { /// <summary> /// Inherit from the member. /// </summary> Inherit, /// <summary> /// Is public access. /// </summary> Public, /// <summary> /// Is internal access. /// </summary> Internal, /// <summary> /// Is protected access. /// </summary> Protected, /// <summary> /// Is private access. /// </summary> Private } /// <summary> /// The event assignment /// </summary> //[System.DefaultValue(Add)] public enum EventAssignable { /// <summary> /// To add the event /// </summary> Add, /// <summary> /// To remove the event /// </summary> Remove, /// <summary> /// To set the event /// </summary> Assign } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Hsu.Sg.FluentMember
Leave a Reply