Can't have a Bogus FinishWith configured
See original GitHub issueSince Bogus allows only a single FinishWith method, the way AutoBogus works its magic makes it impossible for someone to configure a FinishWith for a Type like this:
public class PersonBuilder : AutoFaker<Person>
{
// ... props
}
// ...
var personBuilder = new PersonBuilder().FinishWith((f, p) =>
{
// something
})
.Generate();
This is common in a scenario where a property doesn’t have a setter and has to be configured through methods. So something like this:
public class PersonBuilder : AutoFaker<Person>
{
public PersonBuilder WithAddresses(int count = 1)
{
// store how many addresses must be generated
}
public PersonBuilder WithEmails(int count = 1)
{
// store how many emails must be generated
}
// other methods for Collections without setter
public override Generate(string ruleSets = null)
{
FinishWith((f, p) =>
{
// Calls methods that populate Addresses, Emails, etc.
});
}
}
From what I looked at the code, the culprit is this line.
I see a few ways this could be fixed: Bogus could allow multiple finalizers (I don’t see why this isn’t allowed anyway), or AutoBogus could execute that code in a different way.
What do you think? Is this something that should be improved?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Bogus
Bogus is a simple fake data generator for .NET languages like C#, F# and VB.NET. Bogus is fundamentally a C# port of faker.js...
Read more >Bogus : Simple fake data tool | Sacha's Blog - WordPress.com
Bogus : Simple fake data tool ... //Set the randomzier seed if you wish to generate repeatable data sets. ... //Use an enum...
Read more >Difficulty using usethis::use_github() - tidyverse
Following the documentation for the function, I first setup git using usethis::use_git(). ... repo_name),; paste0("Description: ", repo_desc),; copy = FALSE ...
Read more >URLSession background fails with t…
sessionSendsLaunchEvents = false configuration. ... but it only for testing, I can't really detect when the app has been quit from the app...
Read more >Pixel setup in progress. Tap to finish. Showing for a few days
Tap to finish. ... For the past few days my phone has been displaying "Pixel setup in progress. ... false. Enable Dark Mode....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey @metareven
Thanks for reminding me of this one. I had made the changes a while ago, just hadn’t release it.
This capability has now been included in v2.8.
Nick.
Hey @nickdodd79 @akamud,
I really apologize for the delay. I’ve been busy preparing for job interviews at a few places. Also been thinking a lot about moving Bogus to the .NET Foundation as part of their new Project Maturity Model announced early this week at .NET Conf; mostly as a move to gain more users and as a way of establishing confidence for bigger businesses. @nickdodd79 , if you’d like to be a part of it and you think it’s right for you, please send me an email and we can talk more about it. Essentially, PML Level 3 and 4 require more than 1 maintainer on the project.
As for Bogus having multiple
.FinishWithmethods, I’m not sure. The conceptional API usage forFaker<T>is that the last-call wins. IE:Faker<T>.RuleFor(x.Prop, f => 1)and then if the same rule for the member is specified again,Faker<T>.RuleFor(x.Prop, f => 2)thatf => 2wins andf =>1rule is over-written.The same conceptual API consistency applies to
.FinishWith( f, t =>); last-call wins.At least from Bogus’ point of view; I’m not sure how helpful keeping track of past rules is important to
Faker<T>other than in this specific case here.Faker<T>to allow this implementation detail scenario? I’m not sure yet but inclined to think maybe not, but I’m open to discussing more. My mind can always be changed tho.I like method 3 that you mentioned as a possible workaround for this issue is for
AutoBogusto store a reference to any finalizers being kept track inFaker<T>and call them afterAutoBogusis done hookingFinishWith. For sure, I can help with this by opening up some kind of accessor to a member if anything is private. But it seems like we already have thatprotectedaccessor set: https://github.com/bchavez/Bogus/blob/master/Source/Bogus/Faker[T].cs#L54PR if you need anything more opened if it could help your implementation.
Lastly, another possibility if we want to get crazy hard-core about it; any
Actionis really adelegateunder the hood; specifically, a multi-cast delegate? So, as a crazy C# hack, you technically can combine twoActionmethods in C#; like this check it out:IIRC, under the hood, the C# compiler will use
Delegate.Combine(a,b)to producec. Thereby hacking your way around this neat little problem of needing to keep track of a ‘list’ of invocations. 😈 Specifcally, get theFinalizeAction.Actionand attach your custom hook … might work?Let me know your thoughts.
Thanks, Brian
🗻 🤽♂️ Creedence Clearwater Revival - Green River