My two cents.
This is useful in Unit testing an immutable entity to be used with Builder Pattern.
Say you have Employee domain object defined in an immutable way. We do this typically when [want to adhere to DDD style](
[To see links please register here]
).
```cs
public class Employee
{
public Employee(int id, string firstname, string lastname, DateTime birthdate, string street)
{
this.ID = id;
this.FirstName = firstname;
this.LastName = lastname;
this.BirthDate = birthdate;
this.Street = street;
}
public int ID { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public DateTime BirthDate { get; private set; }
public string Street { get; private set; }
public string getFullName()
{
return this.FirstName + " " + this.LastName;
}
public int getAge()
{
DateTime today = DateTime.Today;
int age = today.Year - BirthDate.Year;
if (BirthDate > today.AddYears(-age))
age--;
return age;
}
}
```
Now you can have an employee builder like the following(inside of the test project). Notice in the end, we have this implicit operator.
```cs
public class EmployeeBuilder
{
private int id = 1;
private string firstname = "first";
private string lastname = "last";
private DateTime birthdate = DateTime.Today;
private string street = "street";
public Employee Build()
{
return new Employee(id, firstname, lastname, birthdate, street);
}
public EmployeeBuilder WithFirstName(string firstname)
{
this.firstname = firstname;
return this;
}
public EmployeeBuilder WithLastName(string lastname)
{
this.lastname = lastname;
return this;
}
public EmployeeBuilder WithBirthDate(DateTime birthdate)
{
this.birthdate = birthdate;
return this;
}
public EmployeeBuilder WithStreet(string street)
{
this.street = street;
return this;
}
public static implicit operator Employee(EmployeeBuilder instance)
{
return instance.Build();
}
}
```
Now you can have a employee test class like the following.
```cs
public class EmployeeTest
{
[Test]
public void GetFullNameReturnsCombination()
{
// Arrange
Employee emp = new EmployeeBuilder().WithFirstName("Vivek").WithLastName("Koppula");
// Act
string fullname = emp.getFullName();
// Assert
Assert.That(fullname, Is.EqualTo("Vivek Koppula"));
}
[Test]
public void GetAgeReturnsCorrectValue() {
// Arrange
Employee emp = new EmployeeBuilder().WithBirthDate(new DateTime(1983, 1,1));
// Act
int age = emp.getAge();
// Assert
Assert.That(age, Is.EqualTo(DateTime.Today.Year - 1983));
}
}
```
This makes writing unit tests easier by enabling us to construct the employee just with required parameters.
For example in the first test, we are only concerned with first name and last name. So for the first case, we don't have to be bothered by age and street.
Similarly for the second case, we are concerned with age and nothing else.
Article References.
1. [flexible-and-expressive-unit-tests-with-the-builder-pattern](
[To see links please register here]
)
2. [improve-tests-with-the-builder-pattern-for-test-data](
[To see links please register here]
)