I am bit new to C# and some of its advanced OOP features.
I am using the Bogus package to generate synthetic data.
Below is a sample code that picks a random value from the parameter list (EventActionsDomainValues) and assigns it to an attribute in a class (EventSchema) using the synthetic data generation rule defined for this attribute in syntheticEvent object.
However, I need to make this code fully parameterized as follows:
- The attribute name (EventAction) for class EventSchema cannot be hard coded. It needs to be read from some string parameter.
public class EventSchema
{
public string? EventAction { get; set; }
public override string ToString()
{
return $"{EventAction} ";
}
}
- The attribute name (EventAction) and random values input (EventActionsDomainValues) into the synthetic data generation rule cannot be hard coded. They need to be read from some rule string parameters.
var syntheticEvent = syntheticEventFaker
.RuleFor(de => de.EventAction, f => f.PickRandom(EventActionsDomainValues))
.Generate();
Current code with hard coded classes:
using Bogus;
public class StaticClasses
{
public static void Main()
{
// EventActionsDomainValues parameter.
List<string> EventActionsDomainValues = new List<string>
{
"Post", "Patch", "Put", "Get", "Delete"
};
var syntheticEventFaker = new Faker<EventSchema>();
var syntheticEvent = syntheticEventFaker
.RuleFor(de => de.EventAction, f => f.PickRandom(EventActionsDomainValues))
.Generate();
Console.WriteLine(syntheticEvent.ToString());
}
public class EventSchema
{
public string? EventAction { get; set; }
public override string ToString()
{
return $"{EventAction} ";
}
}
}
Need help/guidance with points 1 and 2 above. How would the parameterized code look?
You can generate dynamic members and add and remove them at runtime with the ExpandoObject Class. For example: