Coder Social home page Coder Social logo

bnaya.builderpatterngenerator's Introduction

Builder Pattern Code Generation for .NET

NuGet
Deploy

This tool is designed to simplify the process of creating builders pattern for your classes, structs, or records. Unlike traditional builder generation tools, our approach focuses on immutability and ease of use. We've incorporated features that make building objects even more convenient and user-friendly.

Table of Contents

Introduction

The Builder Pattern Code Generation tool is designed with the modern developer in mind. It automates the process of creating builder patterns for your types while promoting immutability and a user-friendly experience. The tool exposed steps of building the mandatory information before proceeding to optional steps, ensuring your objects are built with all the necessary data.

Features

  • Immutability: The generated builders follow an immutable approach, ensuring the integrity of your objects, and reusable of a builder for creating a multiples lightly different objects.
  • Step-by-Step Building: The builder guides you through the process, requiring mandatory fields before proceeding to optional ones.
  • Hidden Used Stages: Already used stages are hidden, streamlining the building process and reducing complexity.
  • Clean and Readable Code: The generated code is well-organized and easy to understand, making maintenance a breeze.
  • Saves Development Time: Automating builder creation saves you time and effort, allowing you to focus on Type's data rather on the Builder techniques.

Getting Started

To start using the Builder Pattern Code Generation tool, follow these simple steps:

  1. Installation: Include the NuGet of the builder generation and the abstraction libraries:
dotnet add package BuilderPatternGenerator 
dotnet add package BuilderPatternGenerator.Abstractions
  1. Decorate Your Type: Mark the class, struct, or record you want to generate a builder for with the [GenerateBuilderPattern] attributes. This signals the generator to create the builder for your type.

Usage

Using the generated builder is intuitive and straightforward. Here's a basic example:

// PersonBuilder.cs
[GenerateBuilderPattern]
public partial record Person(int Id, string Name)
{
    public required string Email { get; init; }
    public DateTime Birthday { get; init; }
}

Don't forget to mark the Type as partial

[Fact]
public void PersonBuilder_Test()
{
    DateTime dateTime = DateTime.Now.AddYears(-32);
    var p1 = Person.CreateBuilder()
                    .AddName("Joe")
                    .AddId(3)
                    .AddEmail("[email protected]")
                    .AddBirthday(dateTime)
                    .Build();

    Assert.Equal(p1, new Person(3, "Joe") { Email = "[email protected]", Birthday = dateTime });
}

Examples

For more comprehensive examples, check out the Bnaya.BuilderPatternGenerator.SrcGen.Playground project in our GitHub repository. These examples cover various scenarios, demonstrating the power and flexibility of our Builder Pattern Code Generation tool.

bnaya.builderpatterngenerator's People

Contributors

bnayae avatar

Stargazers

 avatar Michał Dolaś avatar Diego Luiz Brum avatar Bnaya & Payoneer avatar Shamal Perera avatar authsol avatar Victorio Berra avatar Oz avatar Frédéric Cantenot avatar Chen Yu Pao avatar  avatar Maximilian Stümpfl avatar Ricardo Ferreira avatar angelofb avatar Iain Rough avatar

Watchers

 avatar

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.