GithubHelp home page GithubHelp logo

godsharp / fluentmember Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 151 KB

Fluent Member Source Generator

License: MIT License

C# 85.41% PowerShell 7.90% Batchfile 0.56% Shell 6.14%
sourcegenerator fluent fluentmember fluent-member csharp-sourcegenerator

fluentmember's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.