Facilitates work with immutable objects, allows changing storage restrictions and prevents importing source objects
Install-Package BuilderImmutableObject
Library to work with C# immutable objects.
public class Car
{
[Obsolete]
public Car()
{
}
public Car(string color, int size)
{
Color = color;
Size = size;
}
public string Color { get; private set; }
public int Size { get; private set; }
}
public class BuilderTest
{
private Car _car;
[SetUp]
public void SetUp()
{
_car = new Car(color: "red", size: 78);
}
[Test]
public void Must_Change_Color_Property()
{
var newCar = _car.Set(x => x.Color, "black").Build();
Assert.AreNotSame(_car, newCar);
Assert.AreEqual(78, _car.Size);
Assert.AreEqual("red", _car.Color);
Assert.AreEqual(78, newCar.Size);
Assert.AreEqual("black", newCar.Color);
}
[Test]
public void Must_Change_All_Properties()
{
var newCar = _car
.Set(x => x.Color, "black")
.Set(x => x.Size, 678)
.Build();
Assert.AreNotSame(_car, newCar);
Assert.AreEqual(78, _car.Size);
Assert.AreEqual("red", _car.Color);
Assert.AreEqual(678, newCar.Size);
Assert.AreEqual("black", newCar.Color);
}
}