
Var generatedVehicles = vehicleGenerator.Generate(NumberOfVehiclesPerEmployee) Var vehicleGenerator = GetVehicleGenerator(employeeId) NET/C# content and get paid? > JOIN US! GetBogusVehicleData(Guid employeeId)
#Fake user data generator code
Wanna join Code Maze Team, help us produce more awesome. Here, we define two static read-only List objects, Employees and Vehicles, which we will use to store our generated data. Public static readonly List Vehicles = new() Our data generators are ready and we can start generating our fake data inside the DataGenerator class.įirst, let’s specify some class members for storing our generated data: public static readonly List Employees = new() In our case, we only use a couple of datasets that Bogus is offering, however, there are many more, like Address, Commerce, Company, Finance, Images, System, etc. We use our GetBogusVehicleData() method, which we will inspect later, to create nested Vehicle objects and populate the value of the Vehicles field for each different Employee. Similarly, the PickRandom() method is used to seed Personality values from our Personality enumeration. In this way, we ensure our data is realistic.Īlso, we use the Int() method from the Randomizer object to populate the YearsOld value within a specific range. Have a look at how we use our FirstName and LastName generated values as input parameters when creating an email address with the Email() function from the Internet dataset. We can see a couple of different Bogus datasets being used in this example, like Name, Address, Internet and Lorem. RuleFor(e => e.Personality, f => f.PickRandom())Īgain, in the GetEmployeeGenerator() method, we create a new Faker object which we will use later for creating fake employees. RuleFor(e => e.AboutMe, f => f.Lorem.Paragraph(1)) RuleFor(e => e.Email, (f, e) => f.Internet.Email(e.FirstName, e.LastName)) RuleFor(e => e.Address, f => f.Address.FullAddress()) RuleFor(e => e.LastName, f => f.Name.LastName()) RuleFor(e => e.FirstName, f => f.Name.FirstName()) NET/C# content and get paid? > JOIN US! GetEmployeeGenerator()

Of course, a different field has different rules in this case:

We specify a different rule for each field of the Vehicle class, with the RuleFor() method. In the GetVehicleGenerator() method, we create a new Faker object from Bogus, which accepts a generic class for which we are specifying the rules, in this case, the Vehicle class. RuleFor(v => v.Fuel, f => f.Vehicle.Fuel()) RuleFor(v => v.Manufacturer, f => f.Vehicle.Manufacturer()) RuleFor(v => v.EmployeeId, _ => employeeId) NET/C# content and get paid? > JOIN US! GetVehicleGenerator(Guid employeeId) We now have our employees with vehicles, but we only have classes. In both Employee and Vehicle classes, we override the ToString() method to serialize and indent our data, to display it nicely later in the article.
