introduce convention based approach to allow default values for properties
I have been working on something of a wrapper around NBuilder in order to accomplish exactly this purpose. Default values is something that I really like about tools like factory-girl, but the .NET versions have some shortcomings that this tool does not.
My approach thus far has been to define factory classes like this:
public class ReasonFactory : NFactory<Reason>
{
public override ISingleObjectBuilder<Reason> Build()
{
return base.Build().WithDefaultValues();
}
public override IListBuilder<Reason> BuildList(int size)
{
return base.BuildList(size).All().HavingDefaultValues();
}
}
public static class ReasonFactoryExtensions
{
public static ISingleObjectBuilder<Reason> WithDefaultValues(this ISingleObjectBuilder<Reason> builder)
{
return builder
.With(r => r.Name = Faker.Lorem.Sentence());
}
public static IListBuilder<Reason> HavingDefaultValues(this IListBuilder<Reason> builder)
{
return builder.All()
.With(r => r.Name = Faker.Lorem.Sentence());
}
}
There are some other classes involved that will take care of registering these factories so that NFactoryGirl.Build<Reason>() and NFactoryGirl.BuildList<Reason>(count) will call the appropriate methods from the Factory classes.
While this is working pretty well, there is a clear problem with repetition. This results from the need to define the same logic for IListBuilder<T> and ISingleObjectBuilder<T>. This obviously comes from the fact that the With methods on IOperable<T> and ISingleObjectBuilder<T> are not from a shared interface.
Do you have any suggestions? Is there a better way to write extension methods such that they can be applied to single objects as well as lists?
Also, I see this issue is a couple years old. Are there any actual plans to introduce this feature? Is there already a general design for it?