55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
|
|
namespace Common.Builders;
|
||
|
|
|
||
|
|
using Domain.Entities;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Builder pattern for creating Subject test objects.
|
||
|
|
/// Encapsulates reflection-based ID and navigation property setting.
|
||
|
|
/// </summary>
|
||
|
|
public class SubjectBuilder
|
||
|
|
{
|
||
|
|
private int _id = 1;
|
||
|
|
private string _name = "Mathematics";
|
||
|
|
private int _professorId = 1;
|
||
|
|
private Professor? _professor;
|
||
|
|
|
||
|
|
public SubjectBuilder WithId(int id)
|
||
|
|
{
|
||
|
|
_id = id;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public SubjectBuilder WithName(string name)
|
||
|
|
{
|
||
|
|
_name = name;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public SubjectBuilder WithProfessorId(int professorId)
|
||
|
|
{
|
||
|
|
_professorId = professorId;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public SubjectBuilder WithProfessor(Professor professor)
|
||
|
|
{
|
||
|
|
_professor = professor;
|
||
|
|
_professorId = professor.Id;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Subject Build()
|
||
|
|
{
|
||
|
|
var subject = new Subject(_name, _professorId);
|
||
|
|
SetProperty(subject, "Id", _id);
|
||
|
|
|
||
|
|
if (_professor is not null)
|
||
|
|
SetProperty(subject, "Professor", _professor);
|
||
|
|
|
||
|
|
return subject;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void SetProperty<T>(T obj, string propertyName, object value) where T : class =>
|
||
|
|
typeof(T).GetProperty(propertyName)!.SetValue(obj, value);
|
||
|
|
}
|