Comments (1)
If you're using Faker<T>
method, then you'll need to extend Faker<T>
to capture the internal Faker
facade (aka FakerHub
) so that setting the seed is flowed to the custom dataset as well.
Ultimately, every Faker
facade has an IHasRandomizer
interface that exposes the internal SeedNotifier
that notifies child datasets when the Faker.Random
izer property has changed.
Basically, what you want to do with a custom dataset is to flow (register) your custom dataset with the internal Faker
facade (aka Faker<T>.FakerHub
) before using it; so that your custom dataset is notified when a seed value is changed or set.
For example,
void Main()
{
var id = 0;
var mealFaker = new ExtendedFaker<Meal>()
.UseSeed(777)
.RuleFor(m => m.Id, f => id++)
.RuleFor(m => m.Drink, f => f.Food().Drink())
.RuleFor(m => m.Candy, f => f.Food().Candy());
mealFaker.Generate(5).Dump();
mealFaker.UseSeed(777); id = 0;
mealFaker.Generate(5).Dump();
}
public class ExtendedFaker<T> : Faker<T> where T : class
{
public ExtendedFaker() : base()
{
// registers Food() custom dataset with Faker facade (aka FakerHub):
this.FakerHub.Food();
// or call
//ContextHelper.GetOrSet(this.FakerHub, () => new Food());
}
}
public class Meal
{
public int Id;
public string Drink;
public string Candy;
}
public static class ExtensionsForFood
{
public static Food Food(this Faker faker)
{
return ContextHelper.GetOrSet(faker, () => new Food());
}
}
public class Food : Bogus.DataSet
{
private static readonly string[] Candies = { "Hard candy", "Taffy", "Chocolate bar", "Stick candy", "Jelly bean", "Mint", "Cotton candy", "Lollipop" };
private static readonly string[] Drinks = { "Soda", "Water", "Beer", "Wine", "Coffee", "Lemonade", "Milk" };
public string Candy() => this.Random.ArrayElement(Candies);
public string Drink() => this.Random.ArrayElement(Drinks);
}
And using Faker
facade only:
void Main()
{
var faker = new Faker();
faker.Food(); // registers Food() custom dataset with faker facade. Or similary call:
//ContextHelper.GetOrSet(faker, () => new Food());
faker.Random = new Randomizer(777);
Enumerable.Range(1,5).Select(_ => faker.Food().Candy()).Dump();
faker.Random = new Randomizer(777);
Enumerable.Range(1,5).Select(_ => faker.Food().Candy()).Dump();
}
Hope that helps.
Thanks,
Brian
from bogus.
Related Issues (20)
- Faker.Tv() is missing from Bogus.Hollywood HOT 3
- Feature request: New localized data: Word(s).
- Add ability to except random numbers HOT 2
- Update Denmark (da) locale from faker-js
- Nuget for .NET 8.0.x HOT 4
- Random history facts generation
- Are Randomizer's number ranges inclusive or exclusive? HOT 3
- bogus is missing NuGet package README file HOT 1
- .NET 8 support HOT 9
- Gender-digit is incorrect for Swedish Personnummer generation HOT 3
- Switch to licence expression rather than URL HOT 1
- Invalid Iban generation for CR (Costa Rica) HOT 3
- Add hungarian (hu) locale from faker-js HOT 2
- How to make convert to generic method HOT 6
- Fakers used inside Rules of other Fakers share the same object reference HOT 2
- Include the increment count for the Rules Expressions HOT 1
- Stack memory issue in locale tests (overflows on Mac) HOT 2
- Update `zh_CN` dataset to latest HOT 3
- private setters workaround is not working for generic types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bogus.