### Basic Builder Setup Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/ReadMe.md Demonstrates how to set up a basic builder class using the `BuilderFor` attribute. The source generator will automatically create builder methods for the properties of the specified type. ```csharp using BuilderGenerator; public record Foo(string Name, int Age); [BuilderFor(typeof(Foo))] public partial class FooBuilder { } ``` -------------------------------- ### BuilderGenerator API Reference Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuilderClass.txt This section details the core components and methods of the BuilderGenerator framework. It covers the generic Builder class, its methods for building objects, and setting properties. ```apidoc BuilderGenerator.Builder - A generic base class for creating builder patterns. - T: The type of the object to be built. Properties: - Placeholder for properties of the target object that can be set via the builder. Methods: Build(): T - Constructs and returns an instance of the target object (T) with the configured properties. - Returns: An instance of type T. With

(P value): {{BuilderClassName}} - Sets a specific property of the target object. - P: The type of the property value. - value: The value to set for the property. - Returns: The current builder instance for method chaining. WithValueFrom

(Func

valueProvider): {{BuilderClassName}} - Sets a specific property of the target object using a function that provides the value. - P: The type of the property value. - valueProvider: A function that returns the value for the property. - Returns: The current builder instance for method chaining. ``` -------------------------------- ### Basic Builder Generation Usage Source: https://github.com/safalin1/buildergenerator2/blob/main/README.md Shows the fundamental steps to use BuilderGenerator2: decorating a partial class with `[BuilderFor]` and rebuilding the project to generate builder methods for class properties. ```csharp [BuilderFor(typeof(Foo))] public partial class FooBuilder { } ``` -------------------------------- ### Factory Methods for Data Scenarios Source: https://github.com/safalin1/buildergenerator2/blob/main/README.md Demonstrates how to define static factory methods within the builder's partial class to create specific data scenarios for testing or other purposes. ```csharp [BuilderFor(typeof(Foo))] public partial class FooBuilder { public static FooBuilder Bar() { return new FooBuilder() .WithBar(true); } public static FooBuilder NotBar() { return new FooBuilder() .WithBar(false); } } ``` -------------------------------- ### Property Value Assignment Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuildMethodSetter.txt This snippet demonstrates a common pattern for assigning a property's value, likely within a configuration or builder context. It suggests retrieving a 'Value' from a property and assigning it to the property itself. ```unknown {{PropertyName}} = {{PropertyName}}.Value, ``` -------------------------------- ### Using Generated Builder with Property Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/WithValuesFromMethodInner.txt This snippet demonstrates how to instantiate and use a generated builder pattern with a specific property. It shows the typical usage pattern after a builder has been generated for a class. ```csharp With{{PropertyName}}(instance.{{PropertyName}}); ``` -------------------------------- ### Generated Builder Class Structure (C#) Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuilderClass.txt This snippet shows the basic structure of a C# class generated by Safalin.BuilderGenerator. It includes namespaces, inheritance from a generic Builder class, and placeholders for properties and methods. ```csharp //---------------------------------------------------------------------------- // // This code was generated by Safalin.BuilderGenerator at {{GeneratedAt}}. // //---------------------------------------------------------------------------- {{BuilderClassUsingBlock}} using System.CodeDom.Compiler; #nullable disable namespace {{BuilderClassNamespace}} { {{BuilderClassAccessibility}} partial class {{BuilderClassName}} : BuilderGenerator.Builder<{{TargetClassFullNameRes}}> { {{Properties}} {{BuildMethod}} {{WithMethods}} {{WithValueFromMethod}} } } ``` -------------------------------- ### Builder with Factory Methods Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/ReadMe.md Shows how to add custom factory methods to a builder class. These methods can pre-configure the builder with specific data scenarios, making it easier to create objects for testing or common use cases. ```csharp using BuilderGenerator; public record Foo(bool IsActive); [BuilderFor(typeof(Foo))] public partial class FooBuilder { public static FooBuilder Active() => new FooBuilder().WithIsActive(true); public static FooBuilder Inactive() => new FooBuilder().WithIsActive(false); } ``` -------------------------------- ### Copying Values with WithValuesFrom Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/ReadMe.md Demonstrates the `WithValuesFrom` method, which allows copying property values from an existing object instance into the current builder. ```csharp using BuilderGenerator; public record Data(string Name, int Value); [BuilderFor(typeof(Data))] public partial class DataBuilder { } // Usage: // var existingData = new Data("Test", 10); // var builder = new DataBuilder().WithValuesFrom(existingData); // builder.WithName("Updated"); // Overrides copied value ``` -------------------------------- ### Copying Values with WithValuesFrom Source: https://github.com/safalin1/buildergenerator2/blob/main/README.md Illustrates the usage of the `WithValuesFrom` method, which enables copying property values from an existing object instance into the builder. ```csharp new MyBuilder().WithValuesFrom(anotherObject); ``` -------------------------------- ### C# Lazy Object Building Method Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuildMethodConstructor.txt This C# method implements lazy initialization for an object of type 'TargetClassFullName'. It ensures the object is created only when first accessed and includes a post-processing step. The constructor of 'TargetClassFullName' is called with specified parameters. ```csharp public override {{TargetClassFullName}} Build() { if (Object?.IsValueCreated != true) { Object = new System.Lazy<{{TargetClassFullName}}>(() => { var result = new {{TargetClassFullName}}( {{Parameters}} ); return result; }); PostProcess(Object.Value); } return Object.Value; } ``` -------------------------------- ### Builder for Record Types Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/ReadMe.md Illustrates the usage of `BuilderFor` attribute with C# record types. The source generator correctly handles records, generating builder methods for their properties. ```csharp using BuilderGenerator; public record MyRecord(bool Hello); [BuilderFor(typeof(MyRecord))] public partial class MyRecordBuilder { } // Usage: // var builder = new MyRecordBuilder().WithHello(true).Build(); ``` -------------------------------- ### Copy Property Values to Builder Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/WithValuesFromMethodOuter.txt This C# method copies all property values from a given instance to the current builder instance. It's a common pattern for initializing or updating a builder with existing data. ```csharp ///

\ /// Copies all property values from and applies it to this builder.\ /// \ public {{BuilderClassName}} WithValuesFrom({{TargetClassFullName}} instance) { // Placeholder for generated property copy methods // Example: this.PropertyName = instance.PropertyName; return this; } ``` -------------------------------- ### Lazy Property Generation in C# Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/PropertyDeclaration.txt This C# code snippet demonstrates the generation of a lazy-initialized property. It uses `System.Lazy` to defer the instantiation of the property's value until it is first accessed. This is useful for performance optimization when the property's value is expensive to compute or create. ```csharp public System.Lazy<{{PropertyType}}> {{PropertyName}} = new System.Lazy<{{PropertyType}}>(() => default({{PropertyType}})); ``` -------------------------------- ### Set Property with Function Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/WithMethod.txt Sets a property using a function that returns the property's value. This leverages lazy initialization for the property. ```csharp /// \ /// Sets {{PropertyName}} to the value returned by .\ /// \ public {{BuilderClassName}} With{{PropertyName}}(System.Func<{{PropertyType}}> func) { {{PropertyName}} = new System.Lazy<{{PropertyType}}>(func); return this; } ``` -------------------------------- ### Build Method in C# Builder Pattern Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuildMethodInitializer.txt This C# code snippet represents the core 'Build' method of a class implementing the Builder pattern. It ensures that the target object is created lazily and only once. The method applies all configured values via setters before returning the fully constructed object. It also includes a hook for post-processing the created object. ```csharp /// \ /// Gets an instance of {{TargetClassName}} with all builder values applied\ /// \ public override {{TargetClassFullName}} Build()\ {\ if (Object?.IsValueCreated != true)\ {\ Object = new System.Lazy<{{TargetClassFullName}}>(() =>\ {\ var result = new {{TargetClassFullName}}\ {\ {{Setters}}\ };\ \ return result;\ });\ \ PostProcess(Object.Value);\ }\ \ return Object.Value;\ } ``` -------------------------------- ### Builder for Record Types Source: https://github.com/safalin1/buildergenerator2/blob/main/README.md Demonstrates how to use the `[BuilderFor]` attribute on C# record types to generate a fluent builder. This allows for easy instantiation and modification of record properties. ```csharp public record MyRecord(bool Hello); [BuilderFor(typeof(MyRecord))] public partial class MyRecordBuilder { } var builder = new MyRecordBuilder().WithHello(true).Build(); ``` -------------------------------- ### Set Property with Value Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/WithMethod.txt Sets a property to a specific value. This method is part of a builder pattern implementation. ```csharp /// \ /// Sets {{PropertyName}} to the provided .\ /// \ public {{BuilderClassName}} With{{PropertyName}}({{PropertyType}} value) { return With{{PropertyName}}(() => value); } ``` -------------------------------- ### Abstract Builder Class Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuilderBaseClass.txt The abstract Builder class serves as a base for all object builder classes within the BuilderGenerator framework. It defines the fundamental structure for building instances of a specified type T, including properties for the object being built and methods for its construction and post-processing. ```csharp //---------------------------------------------------------------------------- // // This code was generated by Safalin.BuilderGenerator. // //---------------------------------------------------------------------------- namespace BuilderGenerator { /// Base class for object builder classes. /// The type of the objects built by this builder. public abstract class Builder where T : class { /// Gets or sets the object returned by this builder. /// The constructed object. #pragma warning disable CA1720 // Identifier contains type name protected System.Lazy Object { get; set; } #pragma warning restore CA1720 // Identifier contains type name /// Builds the object instance. /// The constructed object. public abstract T Build(); protected virtual void PostProcess(T value) { } /// Sets the object to be returned by this instance. /// The object to be returned. /// A reference to this builder instance. public Builder WithObject(T value) { Object = new System.Lazy(() => value); return this; } } } ``` -------------------------------- ### BuilderForAttribute C# Code Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/BuilderForAttribute.txt The `BuilderForAttribute` is a C# attribute used to mark classes for which a fluent builder should be generated. It specifies the target type and an option to include internal members. ```csharp //---------------------------------------------------------------------------- // // This code was generated by Safalin.BuilderGenerator. // //---------------------------------------------------------------------------- namespace BuilderGenerator { /// /// Generates a fluent builder class for the provided type /// /// [BuilderFor(typeof(User))] /// public class UserBuilder /// { /// // Your code here /// } /// /// [System.AttributeUsage(System.AttributeTargets.Class)] public class BuilderForAttribute : System.Attribute { public bool IncludeInternals { get; } public System.Type Type { get; } public BuilderForAttribute(System.Type type, bool includeInternals = false) { this.IncludeInternals = includeInternals; this.Type = type; } } } ``` -------------------------------- ### Set Property to Default Source: https://github.com/safalin1/buildergenerator2/blob/main/src/BuilderGenerator/Templates/WithMethod.txt Resets a property to its default value. This is useful for clearing or re-initializing a property within the builder pattern. ```csharp /// \ /// Sets {{PropertyName}} to its default value.\ /// \ public {{BuilderClassName}} Without{{PropertyName}}() { {{PropertyName}} = new System.Lazy<{{PropertyType}}>(() => default({{PropertyType}})); return this; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.