### Generate Instances Using AutoFaker (Static and Instance) Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Shows how to generate instances of types using the non-generic AutoFaker class. Examples cover both static method calls for on-the-fly generation and using a configured instance for reuse. ```csharp // Static usage AutoFaker.Generate(); AutoFaker.Generate(); // Instance usage var faker = AutoFaker.Create(); faker.Generate(); faker.Generate(); ``` -------------------------------- ### Utilize AutoFaker for Typed Instance Generation and Population Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Illustrates the usage of the generic AutoFaker class for creating and populating specific types. Includes examples of defining rules, using rule sets, and populating existing instances. ```csharp var personFaker = new AutoFaker() .RuleFor(fake => fake.Id, fake => fake.Random.Int()) .RuleSet("empty", rules => { rules.RuleFor(fake => fake.Id, () => 0); }); // Use explicit conversion or call Generate() var person1 = (Person)personFaker; var person2 = personFaker.Generate(); person1.Dump(); person2.Dump(); // Populate an existing instance var person3 = new Person(); personFaker.Populate(person3); person3.Dump(); ``` -------------------------------- ### Applying Conventions for Value Generation Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Utilize the AutoBogus.Conventions package to apply predefined rules for generating values based on type and member names. For example, a string property named 'Email' will automatically use the Faker.Internet.Email() generator. Conventions can also be individually configured or disabled. ```csharp AutoFaker.Configure(builder => { builder.WithConventions(); }); ``` ```csharp AutoFaker.Configure(builder => { builder.WithConventions(config => { config.FirstName.Enabled = false; // Disables the FirstName generator config.LastName.AlwaysGenerate = true; // Overrides any LastName values previously generated config.Email.Aliases("AnotherEmail"); // Generates an email value for members named AnotherEmail }); }); ``` -------------------------------- ### Configure AutoBogus Globally and for Faker Instances Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Demonstrates how to configure AutoBogus globally and for specific AutoFaker instances using a builder pattern. This includes settings like locale, repeat count, recursive depth, and member skipping. ```csharp AutoFaker.Configure(builder => { builder .WithLocale() // Configures the locale to use .WithRepeatCount() // Configures the number of items in a collection .WithDataTableRowCount() // Configures the number of data table rows to generate .WithRecursiveDepth() // Configures how deep nested types should recurse .WithTreeDepth() // Configures the tree depth of an object graph .WithBinder() // Configures the binder to use .WithFakerHub() // Configures a Bogus.Faker instance to be used - instead of a default instance .WithSkip() // Configures members to be skipped for a type .WithOverride(); // Configures the generator overrides to use - can be called multiple times }); // Configure a faker var faker = AutoFaker.Create(builder => ...); // Configure a generate request faker.Generate(builder => ...); faker.Generate(builder => ...); AutoFaker.Generate(builder => ...); AutoFaker.Generate(builder => ...); // Configure an AutoFaker var personFaker = new AutoFaker() .Configure(builder => ...) .RuleFor(fake => fake.Id, fake => fake.Random.Int()) .Generate(); ``` -------------------------------- ### Inherit AutoFaker and Generate with Constructor Arguments Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Demonstrates how to inherit from AutoFaker to create custom faker classes, including passing constructor arguments during generation using `WithArgs`. ```csharp public class PersonFaker : AutoFaker { public PersonFaker(int id) { RuleFor(fake => fake.Id, () => id); } } var id = AutoFaker.Generate(); // Create an instance and call Generate() var personFaker = new PersonFaker(id); var person1 = personFaker.Generate(); person1.Dump(); // Create a Person instance using AutoFaker.Generate() // If the AutoFaker class needs constructor arguments, they can be passed using WithArgs() var person2 = AutoFaker.Generate(builder => builder.WithArgs(id)); person2.Dump(); ``` -------------------------------- ### Generating Data with Text Templates Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Leverage AutoBogus.Templating to define data generation rules using a text-based templating notation. The GenerateWithTemplate extension method allows you to specify structured data inline, making it easy to create predefined sets of fake data for testing. ```csharp public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Status { get; set; } // Will be auto generated by the underlying AutoBogus generator } var persons = new AutoFaker().GenerateWithTemplate(@" Id | FirstName | LastName 0 | John | Smith 1 | Jane | Jones 2 | Bob | Clark "); ``` -------------------------------- ### Customizing Object Generation with AutoGeneratorOverride Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Implement custom logic for generating specific types by inheriting from AutoGeneratorOverride. This allows you to define precise rules for how certain objects or their properties are populated. The CanOverride method determines if the override applies, and Generate contains the custom generation logic. ```csharp public class PersonOverride : AutoGeneratorOverride { public override bool CanOverride(AutoGenerateContext context) { return context.GenerateType == typeof(Person); } public override void Generate(AutoGenerateOverrideContext context) { // Apply an email value to the person var person = context.Instance as Person; person.Email = context.Faker.Internet.Email(); } } // Register the override AutoFaker.Configure(builder => builder.WithOverride(new PersonOverride())); ``` -------------------------------- ### Skipping Member Generation in AutoBogus Source: https://github.com/nickdodd79/autobogus/blob/master/README.md Configure AutoBogus to skip generating values for specific types or members. This is useful for excluding certain data points from your fake objects. It supports skipping by generic type, typeof, expression for public members, and string for protected/internal members. ```csharp AutoFaker.Configure(builder => { // Types builder .WithSkip
() // Define a generic type .WithSkip(typeof(Country)); // Define a type // Type members builder .WithSkip(person => person.Name); // Define using an expression for public members .WithSkip("Age"); // Define using a string for protected, internal, etc. members .WithSkip(typeof(Person), "Gender"); // Define using a string for protected, internal, etc. members }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.