Skip to content

godsharp/FluentMember

Repository files navigation

GodSharp.FluentMember

Azure DevOps builds (main) GitHub Workflow Status (branch)

Generate extension method to fluent set member for class, strcut, interface.

Package Version

Name NuGet MyGet
GodSharp.FluentMember NuGet MyGet

Get Started

  • Install Package

    <ItemGroup>
      <PackageReference Include="GodSharp.FluentMember" Version="2021.206.26" />
    </ItemGroup>
  • Add Attribute

    [FluentMemberGenerator(Partial = true)]
    public partial class User
    {
        public int Id { get; internal set; }
        public int Age { get; set; }
        internal string Name { get; set; }
        public string Secret { get; private set; }
        public Role Role { get; set; } = new();
    
        [FluentMemberGenerator(Partial = true)]
        public partial struct Address
        {
            public string Country { get; set; }
            public string City { get; set; }
            internal string Postcode { get; set; }
            public string Secret { get; private set; }
        }
    }
  • Build Project

    You will be use fluent api to set member, like With* and Set method by extension method.

    generated code like this:

    //-----------------------------------------------------------------------   -------
    // <auto-generated>
    //     Generated by SourceGenerator generator at 2022/6/26 15:07:32.
    //     Source: User
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System;
    using System.Linq.Expressions;
    using GodSharp.FluentMember;
    
    namespace GodSharpFluentMemberTest;
    
    public partial class User
    {
        internal User WithId(Int32 id)
        {
            this.Id = id;
            return this;
        }
    
        public User WithAge(Int32 age)
        {
            this.Age = age;
            return this;
        }
    
        internal User WithName(String name)
        {
            this.Name = name;
            return this;
        }
    
        public User WithRole(Role role)
        {
            this.Role = role;
            return this;
        }
    
        public User Set<TMember>(Expression<Func<User,TMember>> predicate,  TMember value)
            => TypeSetter.Set(this,predicate,value);
    
    }
    
    public static class UserExtensions
    {
        internal static User WithId(this User user ,Int32 id)
        {
            user.Id = id;
            return user;
        }
    
        public static User WithAge(this User user ,Int32 age)
        {
            user.Age = age;
            return user;
        }
    
        internal static User WithName(this User user ,String name)
        {
            user.Name = name;
            return user;
        }
    
        public static User WithRole(this User user ,Role role)
        {
            user.Role = role;
            return user;
        }
    
        public static User Set<TMember>(this User user,     Expression<Func<User,TMember>> predicate, TMember value)
            => TypeSetter.Set(user, predicate, value);
    
    }

License

MIT