I\'m creating a series of builders to clean up the syntax which creates domain classes for my mocks as part of improving our overall unit tests. My builders essentially pop
I know this is an old question, but I think you can use a simple cast to avoid the abstract BLDR This { get; }
The resulting code would then be:
public abstract class BaseBuilder where BLDR : BaseBuilder
where T : new()
{
public abstract T Build();
protected int Id { get; private set; }
public BLDR WithId(int id)
{
_id = id;
return (BLDR)this;
}
}
public class ScheduleIntervalBuilder :
BaseBuilder
{
private int _scheduleId;
// ...
public override ScheduleInterval Build()
{
return new ScheduleInterval
{
Id = base.Id,
ScheduleId = _scheduleId
// ...
};
}
public ScheduleIntervalBuilder WithScheduleId(int scheduleId)
{
_scheduleId = scheduleId;
return this;
}
// ...
}
Of course you could encapsulate the builder with
protected BLDR This
{
get
{
return (BLDR)this;
}
}