RSCG – Architect.DomainModeling
Domain Modelling -DDD, Entity and more. Here I will show just the builder
This is how you can use Architect.DomainModeling .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Architect.DomainModeling" Version="3.0.2" />
</ItemGroup>
</Project>
The code that you will use is
using Builder;
var pOld = new Person("Andrei", "Ignat");
pOld.MiddleName = "G";
var build = new PersonBuilder()
.WithFirstName(pOld.FirstName)
//.WithMiddleName("") // it is not into the constructor
.WithLastName(pOld.LastName)
;
var pNew = build.Build();
System.Console.WriteLine(pNew.FullName());
System.Console.WriteLine(pOld.FullName());
namespace Builder;
public class Person
{
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string? MiddleName { get; set; }
public string LastName { get; set; }
public string FullName()
{
return FirstName + " " + MiddleName + " "+LastName;
}
}
using Architect.DomainModeling;
namespace Builder;
[DummyBuilder<Person>]
public partial class PersonBuilder
{
}
The code that is generated is
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
#nullable disable
namespace Builder
{
/// <summary>
/// <para>
/// Implements the Builder pattern to construct <see cref="Builder.Person"/> objects for testing purposes.
/// </para>
/// <para>
/// Where production code relies on the type's constructor, test code can rely on this builder.
/// That way, if the constructor changes, only the builder needs to be adjusted, rather than lots of test methods.
/// </para>
/// </summary>
/* Generated */ public partial class PersonBuilder
{
private string FirstName { get; set; } = "FirstName";
public PersonBuilder WithFirstName(string value) => this.With(b => b.FirstName = value);
private string LastName { get; set; } = "LastName";
public PersonBuilder WithLastName(string value) => this.With(b => b.LastName = value);
private PersonBuilder With(Action<PersonBuilder> assignment)
{
assignment(this);
return this;
}
public Builder.Person Build()
{
var result = new Builder.Person(
firstName: this.FirstName,
lastName: this.LastName);
return result;
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Architect.DomainModeling