bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.81k stars 499 forks source link

Record type support #436

Open ImoutoChan opened 2 years ago

ImoutoChan commented 2 years ago

Does this library support c# record types in some way?

Right now I get "No parameterless constructor defined". But records don't have parameterless constructors and shouldn't have it. Is there a way to force the library to populate records through the record constructor? Automapper works with it just fine.

ChristoWolf commented 2 years ago

Is there a way to force the library to populate records through the record constructor?

Yes, just do the following:

  1. On your Faker<T>, call CustomInstantiator.
  2. Similar to that example, specify your constructor in the lambda which is passed in and use the Faker facade in there to inject random values.
  3. No rule definitions are needed then. Simply generate the object as usual.