Giter Site home page Giter Site logo

records's Introduction

Records

GitHub Actions CI status NuGet package

Immutable record types for c#

Installation

Install following nuget packages into your project:

  1. Records
  2. CodeGeneration.Roslyn.Tool

Features

Constructor generating

If we declare class User like this:

    [Record]
    public partial class User
    {
        public Guid Id { get; } = Guid.NewGuid();

        public DateTimeOffset CreatedAt { get; } = DateTimeOffset.UtcNow;

        public string FirstName { get; }

        public string LastName { get; }

        public string? MiddleName { get; }

        public DateTime? BirthDate { get; }
    }

the following constructor would be generated:

        public User(string firstName,
                    string lastName,
                    Guid? id = default(Guid?),
                    DateTimeOffset? createdAt = default(DateTimeOffset?),
                    string? middleName = default(string?),
                    DateTime? birthDate = default(DateTime?))
        {
            FirstName = firstName;
            LastName = lastName;
            if (id != null)
                Id = id.Value;
            if (createdAt != null)
                CreatedAt = createdAt.Value;
            MiddleName = middleName;
            BirthDate = birthDate;
        }

With methods generating

If we specify with parameter in Record attribute like this:

    [Record(with: true)]
    public partial class User
    {
        public Guid Id { get; } = Guid.NewGuid();

        public string FirstName { get; }

        public string LastName { get; }

        public string? MiddleName { get; }
    }

the following methods and constructor would be generated:

        public User(string firstName,
                    string lastName,
                    Guid? id = default(Guid?),
                    string? middleName = default(string?))
        {
            FirstName = firstName;
            LastName = lastName;
            if (id != null)
                Id = id.Value;
            MiddleName = middleName;
        }

        public User WithFirstName(string firstName)
        {
            return new User(firstName, LastName, Id, MiddleName);
        }

        public User WithLastName(string lastName)
        {
            return new User(FirstName, lastName, Id, MiddleName);
        }

        public User WithId(Guid id)
        {
            return new User(FirstName, LastName, id, MiddleName);
        }

        public User WithMiddleName(string? middleName)
        {
            return new User(FirstName, LastName, Id, middleName);
        }

C# 9

Good news!
It seems Records will appear in C# 9

records's People

Contributors

sobolev88 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mikel785

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.