C# – Bogus

By | 03/08/2022

In this post, we will see how to use the library Bogus.net.
But first of all, what is Bogus?
Bogus is a library for generating fake data and it is based on the Faker.js.
Here, we can find all information and the source code.

We start creating a Console application called TestBogus where, we will install Bogus using the command:

Install-Package Bogus


Then, we will add three classes called Person, User and Company, for testing the library.

PERSON

[PERSON.CS]

using System;

namespace TestBogus
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public DateTime BirthDay { get; set; }
        public string Email { get; set; }
        public string Address { get; set;}
    }
}



PersonFaker class is used to generate a list of person:
[PERSONFAKER.CS]

using Bogus;
using System.Collections.Generic;
using System.Linq;

namespace TestBogus
{
    public class PersonFaker
    {
        // definition of a Faker<Person> object
        readonly Faker<Person> faker;
        public PersonFaker()
        {
            faker = new Faker<Person>();
        }

        public List<Person> GetLstPeople ()
        {
            // we can decide the number of objects we want to create
            return faker.Generate(5).ToList();
        }
    }
}



Finally, we modify the file Program.cs:

[PROGRAM.CS]

using System;

namespace TestBogus
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("List of Person");
            PersonFaker objPersonFaker = new PersonFaker();
            objPersonFaker.GetLstPeople().ForEach(person => {
                GetPropertiesNameOfClass(person);
            });
        }

        // Using the reflection, we can read properties and values of a class
        public static void GetPropertiesNameOfClass(object classInput)
        {
            if (classInput!= null)
            {
                Console.WriteLine("----------------------------");
                foreach (var prop in classInput.GetType().GetProperties())
                {
                    Console.WriteLine($"{prop.Name}: {prop.GetValue(classInput, null)}");
                }
                Console.WriteLine("----------------------------");
            }
        }
    }
}



If we run the application, this will be the result:

We can see that it worked but, if we don’t define rules for the Person’s properties, Bogus will apply the default values.
So, in order to have valid data, we have to modify the PersonFaker class:

[PERSONFAKER.CS]

using Bogus;
using System.Collections.Generic;
using System.Linq;

namespace TestBogus
{
    public class PersonFaker
    {
        readonly Faker<Person> faker;
        public PersonFaker()
        {
            faker = new Faker<Person>();
        }

        public List<Person> GetLstPeople ()
        {
            var lstPeople = faker
                    // we define the rules for the properties
                    .RuleFor(p => p.Id, f => f.Random.Number(1, 100))
                    .RuleFor(p => p.Name, f => f.Person.FirstName)
                    .RuleFor(p => p.Surname, f => f.Person.LastName)
                    .RuleFor(p => p.Email, f => f.Person.Email)
                    .RuleFor(p => p.BirthDay, f => f.Person.DateOfBirth)
                    .RuleFor(p => p.Address, f => f.Address.FullAddress());

            return lstPeople.Generate(5).ToList();
        }
    }
}



Now, if we run the application, this will be the result:



USER

[USER.CS]

namespace TestBogus
{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string IP { get; set; }
    }
}



[USERFAKER.CS]

using Bogus;
using System.Collections.Generic;
using System.Linq;

namespace TestBogus
{
    public class UserFaker
    {
        readonly Faker<User> faker;
        public UserFaker()
        {
            faker = new Faker<User>();
        }

        public List<User> GetLstUsers()
        {
            var lstUsers = faker
                    .RuleFor(p => p.Id, f => f.Random.Number(1, 100))
                    .RuleFor(p => p.Username, f => f.Internet.UserName())
                    .RuleFor(p => p.Password, f => f.Internet.Password())
                    .RuleFor(p => p.Email, f => f.Internet.Email())
                    .RuleFor(p => p.IP, f => f.Internet.Ip());

            return lstUsers.Generate(5).ToList();
        }
    }
}



Finally, we modify the file Program.cs:

[PROGRAM.CS]

using System;

namespace TestBogus
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("List of Users");
            UserFaker objUserFaker = new UserFaker();
            objUserFaker.GetLstUsers().ForEach(user => {
                GetPropertiesNameOfClass(user);
            });
        }

        // Using the reflection, we can read properties and values of a class
        public static void GetPropertiesNameOfClass(object classInput)
        {
            if (classInput!= null)
            {
                Console.WriteLine("----------------------------");
                foreach (var prop in classInput.GetType().GetProperties())
                {
                    Console.WriteLine($"{prop.Name}: {prop.GetValue(classInput, null)}");
                }
                Console.WriteLine("----------------------------");
            }
        }
    }
}



We have done and now, if we run the application, this will be the result:



COMPANY

[COMPANY.CS]

using System.Collections.Generic;

namespace TestBogus
{
    public class Company
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public List<Office> Offices { get; set; }
    }

    public class Office
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public int Employees { get; set; }
        
    }
}



[COMPANYFAKER.CS]

using Bogus;
using System.Collections.Generic;
using System.Linq;

namespace TestBogus
{
    public class CompanyFaker
    {
        readonly Faker<Company> faker;
        readonly Faker<Office> fakerOffice;
        public CompanyFaker()
        {
            faker = new Faker<Company>();
            fakerOffice = new Faker<Office>();
        }

        public List<Company> GetLstCompanies()
        {
            var office = fakerOffice
                    .RuleFor(o => o.Id, f => f.Random.Number(1, 4))
                    .RuleFor(o => o.Address, f => f.Address.FullAddress())
                    .RuleFor(o => o.Employees, f => f.Random.Number(20, 100));


            var lstCompanies = faker
                    .RuleFor(p => p.Id, f => f.Random.Number(1, 100))
                    .RuleFor(p => p.CompanyName, f => f.Company.CompanyName())
                    .RuleFor(p => p.Offices, f => fakerOffice.Generate(2));

            return lstCompanies.Generate(3).ToList();
        }
    }
}



Finally, we modify the file Program.cs:

[PROGRAM.CS]

using Newtonsoft.Json;
using System;

namespace TestBogus
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("List of Companies");
            CompanyFaker objCompanyFaker = new CompanyFaker();
            objCompanyFaker.GetLstCompanies().ForEach(company => {
                JsonConvertCompany(company);
            });
        }

        // I have decided to serialize the result in Json in order to show better the list of offices.
        private static void JsonConvertCompany(Company company)
        {
            Console.WriteLine(JsonConvert.SerializeObject(company, Formatting.Indented));
        }
    }
}



We have done and now, if we run the application, this will be the result:



Category: C# Tags:

Leave a Reply

Your email address will not be published.