/// <summary> /// Generates a fake object of <typeparamref name="T"/> using the specified rules in this /// <seealso cref="Faker{T}"/>. /// </summary> /// <param name="ruleSets">A comma separated list of rule sets to execute. /// Note: The name `default` is the name of all rules defined without an explicit rule set. /// When a custom rule set name is provided in <paramref name="ruleSets"/> as parameter, /// the `default` rules will not run. If you want rules without an explicit rule set to run /// you'll need to include the `default` rule set name in the comma separated /// list of rules to run. (ex: "ruleSetA, ruleSetB, default") /// </param> public virtual T Generate(string ruleSets = null) { Func <Faker, T> createRule = null; var cleanRules = ParseDirtyRulesSets(ruleSets); if (string.IsNullOrWhiteSpace(ruleSets)) { createRule = CreateActions[Default]; } else { var firstRule = cleanRules[0]; createRule = CreateActions.TryGetValue(firstRule, out createRule) ? createRule : CreateActions[Default]; } //Issue 143 - We need a new FakerHub context before calling the // constructor. Associated Issue 57: Again, before any // rules execute, we need a context to capture IndexGlobal // and IndexFaker variables. FakerHub.NewContext(); var instance = createRule(this.FakerHub); PopulateInternal(instance, cleanRules); return(instance); }
/// <summary> /// Given an instance of T, populate it with the desired rule sets. /// </summary> protected virtual void PopulateInternal(T instance, string[] ruleSets) { ValidationResult vr = null; if (!IsValid.HasValue) { //run validation vr = ValidateInternal(ruleSets); this.IsValid = vr.IsValid; } if (!IsValid.GetValueOrDefault()) { throw MakeValidationException(vr ?? ValidateInternal(ruleSets)); } var typeProps = TypeProperties; lock (Randomizer.Locker.Value) { //Issue 57 - Make sure you generate a new context //before executing any rules. FakerHub.NewContext(); foreach (var ruleSet in ruleSets) { if (this.Actions.TryGetValue(ruleSet, out var populateActions)) { foreach (var action in populateActions.Values) { typeProps.TryGetValue(action.PropertyName, out MemberInfo member); var valueFactory = action.Action; if (member != null) { var prop = member as PropertyInfo; prop?.SetValue(instance, valueFactory(FakerHub, instance), null); var field = member as FieldInfo; field?.SetValue(instance, valueFactory(FakerHub, instance)); } else // member would be null if this was an RuleForObject. { //Invoke this if this is a basic rule which does not select a property or a field. var outputValue = valueFactory(FakerHub, instance); } } } } foreach (var ruleSet in ruleSets) { if (this.FinalizeActions.TryGetValue(ruleSet, out FinalizeAction <T> finalizer)) { finalizer.Action(this.FakerHub, instance); } } } }
private void PopulateInternal(T instance, string[] ruleSets) { if (!IsValid.HasValue) { //run validation this.IsValid = ValidateInternal(ruleSets).IsValid; } if (!IsValid.GetValueOrDefault()) { throw new InvalidOperationException($"StrictMode validation failure on {typeof(T)}. The Binder found {TypeProperties.Count} properties/fields but have different number of actions rules. Also, check RuleSets."); } var typeProps = TypeProperties; lock (Randomizer.Locker.Value) { //Issue 57 - Make sure you generate a new context //before executing any rules. FakerHub.NewContext(); foreach (var ruleSet in ruleSets) { if (this.Actions.TryGetValue(ruleSet, out var populateActions)) { foreach (var action in populateActions.Values) { typeProps.TryGetValue(action.PropertyName, out MemberInfo member); var valueFactory = action.Action; if (member != null) { var prop = member as PropertyInfo; prop?.SetValue(instance, valueFactory(FakerHub, instance), null); var field = member as FieldInfo; field?.SetValue(instance, valueFactory(FakerHub, instance)); } else // member would be null if this was an RuleForObject. { //Invoke this if this is a basic rule which does not select a property or a field. var outputValue = valueFactory(FakerHub, instance); } } } } foreach (var ruleSet in ruleSets) { if (this.FinalizeActions.TryGetValue(ruleSet, out FinalizeAction <T> finalizer)) { finalizer.Action(this.FakerHub, instance); } } } }
private void PopulateInternal(T instance, string[] ruleSets) { if (!IsValid.HasValue) { //run validation this.IsValid = ValidateInternal(ruleSets).IsValid; } if (!IsValid.GetValueOrDefault()) { throw new InvalidOperationException($"StrictMode validation failure on {typeof(T)}. The Binder found {TypeProperties.Count} properties/fields but have different number of actions rules. Also, check RuleSets."); } var typeProps = TypeProperties; lock (Randomizer.Locker.Value) { //Issue 57 - Make sure you generate a new context //before executing any rules. FakerHub.NewContext(); foreach (var ruleSet in ruleSets) { Dictionary <string, PopulateAction <T> > populateActions; if (this.Actions.TryGetValue(ruleSet, out populateActions)) { foreach (var action in populateActions.Values) { MemberInfo member; typeProps.TryGetValue(action.PropertyName, out member); var valueFactory = action.Action; var prop = member as PropertyInfo; prop?.SetValue(instance, valueFactory(FakerHub, instance), null); var field = member as FieldInfo; field?.SetValue(instance, valueFactory(FakerHub, instance)); } } } foreach (var ruleSet in ruleSets) { FinalizeAction <T> finalizer; if (this.FinalizeActions.TryGetValue(ruleSet, out finalizer)) { finalizer.Action(this.FakerHub, instance); } } } }
/// <summary> /// Only populates an instance of T. /// </summary> public virtual void Populate(T instance) { var useStrictMode = UseStrictMode ?? Faker.DefaultStrictMode; if (useStrictMode && !IsValid.HasValue) { //run validation this.IsValid = Validate(); } if (useStrictMode && !IsValid.GetValueOrDefault()) { throw new InvalidOperationException($"StrictMode validation failure on {typeof(T)}. The Binder found {TypeProperties.Count} properties/fields but have {Actions.Count} actions rules."); } var typeProps = TypeProperties; lock (Randomizer.Locker.Value) { foreach (var kvp in Actions) { MemberInfo member; typeProps.TryGetValue(kvp.Key, out member); var valueFactory = kvp.Value; var prop = member as PropertyInfo; prop?.SetValue(instance, valueFactory(FakerHub, instance), null); var field = member as FieldInfo; field?.SetValue(instance, valueFactory(FakerHub, instance)); } FinalizeAction?.Invoke(this.FakerHub, instance); FakerHub.NewContext(); } }