Skip to content

Commit

Permalink
Readme work, override work
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Feb 8, 2024
1 parent f620f49 commit 37f7169
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,68 @@ 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

```
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<string>();
var dictionary = autoFaker.Generate<Dictionary<int, string>>();
var order = autoFaker.Generate<Order>();
```

- 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<Order>
{
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<Customer>();
}
}
```

- 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.
Expand Down
16 changes: 1 addition & 15 deletions src/Override/AutoFakerOverride.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
using System;
using Bogus;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Generators;

namespace Soenneker.Utils.AutoBogus.Override;

public abstract class AutoFakerOverride<T> : 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;
}

/// <summary>
Expand All @@ -31,9 +20,6 @@ public override bool CanOverride(AutoFakerContext context)
{
bool shouldOverride = context.CachedType.Type == Type;

if (shouldOverride && !_fakerSet)
Faker = context.Faker;

return shouldOverride;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Soenneker.Facts.Local" Version="2.1.206" />
<PackageReference Include="Soenneker.Facts.Local" Version="2.1.207" />
<PackageReference Include="Soenneker.Facts.Manual" Version="2.1.98" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
Expand Down

0 comments on commit 37f7169

Please sign in to comment.