From 37f7169ee2cbc084ce5dfd7a0db079a362a79754 Mon Sep 17 00:00:00 2001 From: Jake Soenneker Date: Thu, 8 Feb 2024 17:21:48 -0600 Subject: [PATCH] Readme work, override work --- README.md | 57 ++++++++++++++++++- src/Override/AutoFakerOverride.cs | 16 +----- .../Soenneker.Utils.AutoBogus.Tests.csproj | 2 +- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c2f1bab..9cedec3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ It's a replacement for the abandoned [AutoBogus](https://github.com/nickdodd79/A The goals are to be *fast*, and support the latest types in .NET. -.NET 6+ is supported. +.NET 6+ is supported. ## Installation @@ -19,7 +19,60 @@ The goals are to be *fast*, and support the latest types in .NET. dotnet add package Soenneker.Utils.AutoBogus ``` -⚠️ A Bogus `Faker` takes a long time to initialize. It's recommended that a single instance of `AutoFaker` be used if possible. The static usage of `AutoFaker.Generate<>()` should be avoided (as it creates a new `Faker`), but is available. +## Usage + +- Create an AutoFaker instance: +```csharp +var autoFaker = new AutoFaker(); +``` + +- Call `Generate<>()` on any type you want: + +```csharp +var someRandomWord = autoFaker.Generate(); +var dictionary = autoFaker.Generate>(); +var order = autoFaker.Generate(); +``` + +- Set a faker, configuration, rulesets, etc: + +```csharp +autoFaker.Config.Faker = new Faker("de"); +autoFaker.Config.RecursiveDepth = 3; +... +``` + +- `AutoFakerOverride` can be used for type customization: + +```csharp +public class OrderOverride : AutoFakerOverride +{ + public override void Generate(AutoFakerOverrideContext context) + { + var target = (context.Instance as Order)!; + target.Id = 123; + + // Faker is available + target.Name = context.Faker.Random.Word(); + + // AutoFaker is also available + target.Customer = context.AutoFaker.Generate(); + } +} +``` + +- Configuring `AutoFakerOverride` on the `AutoFaker`: + +```csharp +autoFaker.Configure(builder => +{ + builder.WithOverride(new OrderOverride()); +}); +``` + +## Tips + - ⚠️ Instantiating a Bogus `Faker` takes a substantial amount of time (almost 1ms). An instance of `AutoFaker` will create one `Faker`. Thus, it's recommended that a single instance be used if possible. +- `AutoFaker.GenerateStatic<>()` is available, but should be avoided (as it creates a new `AutoFaker`/`Faker` on each call). ## Notes - This is a work in progress. Contribution is welcomed. diff --git a/src/Override/AutoFakerOverride.cs b/src/Override/AutoFakerOverride.cs index d845799..87b6cb4 100644 --- a/src/Override/AutoFakerOverride.cs +++ b/src/Override/AutoFakerOverride.cs @@ -1,5 +1,4 @@ using System; -using Bogus; using Soenneker.Utils.AutoBogus.Context; using Soenneker.Utils.AutoBogus.Generators; @@ -7,21 +6,11 @@ namespace Soenneker.Utils.AutoBogus.Override; public abstract class AutoFakerOverride : AutoFakerGeneratorOverride { - protected Faker Faker { get; private set; } - protected Type Type { get; } - private readonly bool _fakerSet; - - protected AutoFakerOverride(Faker? faker = null) + protected AutoFakerOverride() { Type = typeof(T); - - if (faker == null) - return; - - Faker = faker; - _fakerSet = true; } /// @@ -31,9 +20,6 @@ public override bool CanOverride(AutoFakerContext context) { bool shouldOverride = context.CachedType.Type == Type; - if (shouldOverride && !_fakerSet) - Faker = context.Faker; - return shouldOverride; } } \ No newline at end of file diff --git a/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj b/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj index 35d0133..7a7239f 100644 --- a/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj +++ b/test/Soenneker.Utils.AutoBogus.Tests/Soenneker.Utils.AutoBogus.Tests.csproj @@ -20,7 +20,7 @@ - +