Skip to content

Commit

Permalink
Examples to extend Bogus.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Feb 27, 2021
1 parent 21e7412 commit 67a312d
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreSeedDb", "EFCoreSeedD
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "GettingStarted\GettingStarted.csproj", "{4B10A48D-F572-4984-8C61-8598E792436A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtendingBogus", "ExtendingBogus\ExtendingBogus.csproj", "{44E22B4C-4280-4329-9A7E-3DEC7D487595}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -41,6 +43,18 @@ Global
{4B10A48D-F572-4984-8C61-8598E792436A}.Release|x64.Build.0 = Release|Any CPU
{4B10A48D-F572-4984-8C61-8598E792436A}.Release|x86.ActiveCfg = Release|Any CPU
{4B10A48D-F572-4984-8C61-8598E792436A}.Release|x86.Build.0 = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|x64.ActiveCfg = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|x64.Build.0 = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|x86.ActiveCfg = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Debug|x86.Build.0 = Debug|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|Any CPU.Build.0 = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|x64.ActiveCfg = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|x64.Build.0 = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|x86.ActiveCfg = Release|Any CPU
{44E22B4C-4280-4329-9A7E-3DEC7D487595}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 13 additions & 0 deletions Examples/ExtendingBogus/ExtendingBogus.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="33.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions Examples/ExtendingBogus/ExtensionsForAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ExtendingBogus
{
/// <summary>
/// Augment the existing <seealso cref="Bogus.DataSets.Address"/> DataSet via C# extension method.
/// </summary>
public static class ExtensionsForAddress
{
private static readonly string[] CanadaDowntownTorontoPostalCodes =
{
"M5S", "M5B", "M5X", "M5V", "M4W", "M4X", "M4Y",
"M5A", "M5C", "M5T", "M5E", "M5G", "M5H", "M5J",
"M5K", "M5L", "M6G"
};

public static string DowntownTorontoPostalCode(this Bogus.DataSets.Address address)
{
return address.Random.ArrayElement(CanadaDowntownTorontoPostalCodes);
}
}
}
18 changes: 18 additions & 0 deletions Examples/ExtendingBogus/ExtensionsForTesting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Newtonsoft.Json;

namespace ExtendingBogus
{
public static class ExtensionsForTesting
{
public static void Dump(this object obj)
{
Console.WriteLine(obj.DumpString());
}

public static string DumpString(this object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}
}
}
43 changes: 43 additions & 0 deletions Examples/ExtendingBogus/FoodDataSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Bogus;
using Bogus.Premium;

namespace ExtendingBogus
{
/// <summary>
/// The following shows how to create a dedicated DataSet accessible via C# extension method.
/// </summary>
public static class ExtensionsForFood
{
public static Food Food(this Faker faker)
{
return ContextHelper.GetOrSet(faker, () => new Food());
}
}

/// <summary>
/// This DatSet can be created manually using `new Candy()`, or by fluent extension method via <seealso cref="ExtensionsForFood"/>.
/// </summary>
public class Food : DataSet
{
private static readonly string[] Candies =
{
"Hard candy", "Taffy", "Chocolate bar", "Stick candy",
"Jelly bean", "Mint", "Cotton candy", "Lollipop"
};

/// <summary>
/// Returns some type of candy.
/// </summary>
public string Candy()
{
return this.Random.ArrayElement(Candies);
}

private static readonly string[] Drinks = { "Soda", "Water", "Beer", "Wine", "Coffee", "Lemonade", "Milk" };
public string Drink()
{
return this.Random.ArrayElement(Drinks);
}
}

}
27 changes: 27 additions & 0 deletions Examples/ExtendingBogus/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Bogus;

namespace ExtendingBogus
{
class Program
{
static void Main(string[] args)
{
var userFaker = new Faker<User>()
//Extend Bogus with a 'new' Food data set; see FoodDataSet.cs
.RuleFor(p => p.FaveCandy, f => f.Food().Candy())
.RuleFor(p => p.FaveDrink, f => f.Food().Drink())
//Extend the existing Address data set with a custom C# extension method; see ExtensionsForAddress.cs
.RuleFor(p => p.PostCode, f => f.Address.DowntownTorontoPostalCode());

var user = userFaker.Generate();
user.Dump();
}
}

public class User
{
public string FaveDrink;
public string FaveCandy;
public string PostCode;
}
}
37 changes: 37 additions & 0 deletions Examples/ExtendingBogus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[1]:https://github.com/bchavez/Bogus#the-great-c-example

## Getting Started with Bogus

#### Requirements
* **.NET Core 3.1** or later

#### Description

The `ExtendingBogus` example shows how to extend **Bogus**' APIs in the following ways:

1. Using a custom C# extension method; see `ExtensionsForAddress.cs`.

Augmenting **Bogus**' APIs via C# extension is useful to make APIs cleaner and more suitable for your specific situation.

1. Using a custom data set; see `FoodDataSet.cs`.

Creating a custom data set is useful when categorizing many 'related' APIs together.

To run the example, perform the following commands *inside* this `ExtendingBogus` folder:

* `dotnet restore`
* `dotnet build`
* `dotnet run`

After the `dotnet` commands are successfully executed above, you should see some extended *custom* fake data printed to the console!

```
> dotnet run
```
```json
{
"FaveDrink": "Soda",
"FaveCandy": "Jelly bean",
"PostCode": "M4W"
}
```
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ User Created! Id=0
|:---------:| ----------- |
| **C#** | [**Full working example of 'The Great C# Example'**](https://github.com/bchavez/Bogus/tree/master/Examples/GettingStarted) |
| **C#** | [**Using Bogus and EF Core to a seed database**](https://github.com/bchavez/Bogus/tree/master/Examples/EFCoreSeedDb) |
| **C#** | [**Extending Bogus with custom APIs and data**](https://github.com/bchavez/Bogus/tree/master/Examples/ExtendingBogus) |
| **F#** | [**Using Bogus with F#**](#the-fabulous-f-examples) |
| **VB.NET** | [**Using Bogus with VB.NET**](#the-very-basic-vbnet-example) |

Expand Down

0 comments on commit 67a312d

Please sign in to comment.