Skip to content

bunnypro/DotnetSimpleFactory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleFactory

Nuget License

This library provide a simple object factory for generating data using Bogus as fake data generator.

Installation

dotnet add package Bunnypro.SimpleFactory

Usage Example

using Bunnypro.SimpleFactory;

// Register Factory
Factory.Register<Person>(faker => new Person
{
    Name = faker.Name.FullName(),
    Phone = faker.Phone.PhoneNumber(),
    Email = faker.Internet.Email()
});

// Generate Data
IEnumerable<Person> people = Factory.Create<Person>(4);
Person[] arrayPeople = people.ToArray();
List<Person> listPeople = people.ToList();

// Generate Unique Data
IEnumerable<Person> people = Factory.CreateUnique<Person>(4);

// Generate One Data
Person person = Factory.CreateOne<Person>();

// Extending Data
static const email = "john@doe.com";

IEnumerable<Person> people = Factory.CreateUnique<Person>(4, (person, faker) =>
{
    person.Email = email;

    return person;
});


// Register With Generate Nested Data
Factory.Register<Schedule>(faker => new Schedule
{
    People = Factory.CreateUnique<Person>(4).ToList(),
    Date = faker.Date
});

IEnumerable<Schedule> schedules = Factory.Create<Schedule>(10);


// Check Registered Factory Existence
bool personFactoryExists = Factory.Has<Person>();

// Unregister Factory
bool personFactoryRemoved = Factory.Remove<Person>();

// Clear Factory
Factory.Clear();

// Generate Without Register
double[] doubles = Factory.Once<double>(
    faker => Math.Round(faker.Random.Double(2, 4), 2) // generate double with two decimal places
).Create(4).ToArray();


// Or Another Way
Factory<double> DoubleFactory = new Factory<double>(
    faker => Math.Round(faker.Random.Double(2, 4), 2)
);

double[] doubles = DoubleFactory.Create(4).ToArray();

Factory<Person> PersonFactory = new Factory<Person>(faker => new Person
{
    FullName = faker.Person.FullName,
    Address = faker.Person.Address
});

// same as
Factory<Person> PersonFactory = Factory.Once<Person>(faker => new Person
{
    FullName = faker.Person.FullName,
    Address = faker.Person.Address
});

var people = PersonFactory.Create(100);

About

A Simple Dotnet Library For Generating Data

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages