New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Auto Populate by Convention? #8
Comments
I believe this is great idea. I would very much welcome it. |
I've just implemented a helper class for using attributes for generating rules directly from entities. Just going to use this extensively in a very big app. Here an example: [Fixtures(Locale = "it", Strict = true)]
... |
Hi. This sounds great are there any examples/documentations based on this helper ? |
As the Faker has virtual methods for generate/validate/etc., this might be best to implement as something like inheritance from Faker (ex. AutoFaker), with overrides of those methods? Edit: void Main()
{
var clients = new AutoFaker<Test>()
.RuleFor(u => u.Something, f => f.Finance.Account());
clients.Generate().Dump();
}
public class AutoFaker<T> : Faker<T>
where T : class
{
private string _autofakerset;
public AutoFaker()
{
_autofakerset = Guid.NewGuid().ToString();
var properties = typeof(T).GetProperties();
var firstname = properties.SingleOrDefault(p => "FirstName".Equals(p.Name, StringComparison.OrdinalIgnoreCase));
if (firstname != null)
{
var propName = firstname.Name;
base.Actions.Add(_autofakerset, propName, new PopulateAction<T>
{
Action = (a, b) => base.FakerHub.Person.FirstName,
RuleSet = currentRuleSet,
PropertyName = propName
});
}
}
public override T Generate(string ruleSets = null)
{
var instance = base.Generate(_autofakerset);
base.Populate(instance, ruleSets);
return instance;
}
}
public class Test
{
public string FirstName { get; set; }
public string Something { get; set; }
} |
Actual I think a better approach would be to have another method on the Faker class, similar to RuleFor, where we can extend the Faker with a ruleset. This will not break the "pipeline", and could therefore be better with other features.
I don't see that this would be "mixable" with inheritance. But if the Faker had another method, that could take a set with rules, including predefined. This might be feasible. Ex. : //Need better namings, cleanup, etc.
void Main()
{
var common = new CommonNames();
var clients = new Faker<Test>()
.RuleForKnown(common) //Something like this.
.RuleFor(u => u.Something, f => f.Finance.Account());
//could add something like:
//.RulesForKnown(new DummyValues())
//.RulesForKnown(new DataAnotations())
clients.Generate().Dump();
}
public class Faker<T> : Bogus.Faker<T> //Implement body directly in Bogus.Faker<T>
where T : class
{
public Faker<T> RuleForKnown(CommonNames ruleSet)
{
if (ruleSet == null)
return this;
var properties = typeof(T).GetProperties();
foreach (var rule in ruleSet.GetRules())
foreach (var property in rule.Key.Invoke(properties))
base.Actions.Add(base.currentRuleSet, property.Name, new Bogus.PopulateAction<T>
{
Action = (a, b) => rule.Value(FakerHub),
RuleSet = base.currentRuleSet,
PropertyName = property.Name
});
return this;
}
}
public class CommonNames
{
private readonly Dictionary<Func<PropertyInfo[], PropertyInfo[]>, Func<Bogus.Faker, object>> _rules;
public CommonNames()
{
_rules = new Dictionary<Func<PropertyInfo[], PropertyInfo[]>, Func<Bogus.Faker, object>>();
_rules[Filter("FirstName")] = f => f.Person.FirstName;
_rules[Filter("LastName")] = f => f.Person.LastName;
_rules[Filter("SurName")] = f => f.Person.LastName;
}
public Func<PropertyInfo[], PropertyInfo[]> Filter(string name)
=> propertyInfo => FilterPropertyName(propertyInfo, name ?? string.Empty);
private PropertyInfo[] FilterPropertyName(PropertyInfo[] properties, string name)
=> properties
.Where(p => name.Equals(p.Name, StringComparison.OrdinalIgnoreCase))
.ToArray();
public IEnumerable<KeyValuePair<Func<PropertyInfo[], PropertyInfo[]>, Func<Bogus.Faker, object>>> GetRules()
=> _rules.AsEnumerable();
}
public class Test
{
public string FirstName { get; set; }
public string LASTNAME { get; set; }
public string surname { get; set; }
public string Something { get; set; }
} |
I like your ideas @oddbear the last example almost looks like a good starting point for a decent implementation. Some other API names I like (in order):
Then maybe have
|
Hi Everyone, A special member of our community, @nickdodd79, is working on the core implementation of Bogus' Auto Population feature. Soon you'll be able to create instances of If you'd like to comment on the public API that is currently being formulated for this feature, please let us know in #74. We'd like to hear from everyone so we can get an over-arching view of how you'd expect to use the auto-population features in your projects. Many thanks, |
@bchavez looks like this one should be closed? |
@SimonCropp sounds good to me. For those looking for an automatic solution, please checkout AutoBogus: |
Just thought:
Possibly have a method that can auto-populate a domain/DTO object if certain property conventions are met.
IE: when auto populating an object, if properties such as
SSN
andPhone
orTelephone
are encountered, auto populate them without having to setup fluent rules for that property.I think it would reduce the amount of setup required to get started.
The text was updated successfully, but these errors were encountered: