nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
444 stars 49 forks source link

Best way to generate a list of strings with applied rules? #14

Closed McPhale closed 5 years ago

McPhale commented 5 years ago

There's probably something pretty basic I'm missing here, apologies in advance :)

Let's say I want to generate a list of 10 strings formatted as email addresses. Is there an optimum way of doing this?

//This creates a list of 10 null strings; not really what I'm after :p
var stringList = new AutoFaker<string>().Generate(10);

//This creates a list of 10 strings, but I can't seem to find a way to apply rules to them.
var stringList2 = AutoFaker.Generate<string>(10);

//This works but now I've got an extra container class to worry about.
var emailList = new AutoFaker<emailContainer>().RuleFor(c => c.email, f => f.Internet.Email()).Generate(10);

class emailContainer
{
     public string email { get; set; }
}   
nickdodd79 commented 5 years ago

Hi @McPhale

Currently, the way you are doing it is the way I suggest. I am however working on some enhancements that will allow you apply these rules more generically either in your own IAutoBinder instance or via a new Override mechanism I am adding. This will essentially allow you to intercept the generate process for a given type or name (e.g. reflected property name) and provide the value yourself.

Furthermore, this provides an opportunity to add a library that compliments the main AutoBogus core whereby common mappings like Name, Email, Address, etc. can be resolved without any further configuration on the consuming code. Essentially, it is a set of conventions use to generate types and will be available as a separate NuGet Package.

I am hoping to push all this out in the next few weeks.

Nick.

bchavez commented 5 years ago

Hey friends,

I'm not sure if this helps, but don't forget you can generate an email list like this too:

(using latest Bogus v26.0.2)

var emailList = Enumerable.Range(0, 10)
                  .Select(i => new Person(seed:i))
                  .Select(p => p.Email);
emailList.Dump();

and if determinism isn't important, you can reduce it even more:

var emailList  = Enumerable.Range(0, 10)
                   .Select(i => new Person().Email);

LINQPad_2855

BTW, keep up the amazing work you're doing here Nick! I'm always excited to see the progress you're making here!

Also, super big thank you for your AutoBogus work and contribution to the community! I think life would be a lot harder for developers without it!

McPhale commented 5 years ago

Thanks guys! Makes sense to me