From ae40cadad5ad31782c26536cf8a21ac1abd8ad74 Mon Sep 17 00:00:00 2001 From: bchavez Date: Fri, 26 Feb 2021 16:55:44 -0800 Subject: [PATCH] Examples to extend Bogus. --- Examples/Examples.sln | 14 ++++++ Examples/ExtendingBogus/ExtendingBogus.csproj | 13 ++++++ .../ExtendingBogus/ExtensionsForAddress.cs | 20 +++++++++ .../ExtendingBogus/ExtensionsForTesting.cs | 18 ++++++++ Examples/ExtendingBogus/FoodDataSet.cs | 43 +++++++++++++++++++ Examples/ExtendingBogus/Program.cs | 27 ++++++++++++ Examples/ExtendingBogus/README.md | 37 ++++++++++++++++ README.md | 1 + 8 files changed, 173 insertions(+) create mode 100644 Examples/ExtendingBogus/ExtendingBogus.csproj create mode 100644 Examples/ExtendingBogus/ExtensionsForAddress.cs create mode 100644 Examples/ExtendingBogus/ExtensionsForTesting.cs create mode 100644 Examples/ExtendingBogus/FoodDataSet.cs create mode 100644 Examples/ExtendingBogus/Program.cs create mode 100644 Examples/ExtendingBogus/README.md diff --git a/Examples/Examples.sln b/Examples/Examples.sln index 5b48410b..66237028 100644 --- a/Examples/Examples.sln +++ b/Examples/Examples.sln @@ -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 @@ -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 diff --git a/Examples/ExtendingBogus/ExtendingBogus.csproj b/Examples/ExtendingBogus/ExtendingBogus.csproj new file mode 100644 index 00000000..35d6f435 --- /dev/null +++ b/Examples/ExtendingBogus/ExtendingBogus.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + diff --git a/Examples/ExtendingBogus/ExtensionsForAddress.cs b/Examples/ExtendingBogus/ExtensionsForAddress.cs new file mode 100644 index 00000000..dc4ede7c --- /dev/null +++ b/Examples/ExtendingBogus/ExtensionsForAddress.cs @@ -0,0 +1,20 @@ +namespace ExtendingBogus +{ + /// + /// Augment the existing DataSet via C# extension method. + /// + 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); + } + } +} diff --git a/Examples/ExtendingBogus/ExtensionsForTesting.cs b/Examples/ExtendingBogus/ExtensionsForTesting.cs new file mode 100644 index 00000000..06c653d8 --- /dev/null +++ b/Examples/ExtendingBogus/ExtensionsForTesting.cs @@ -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); + } + } +} diff --git a/Examples/ExtendingBogus/FoodDataSet.cs b/Examples/ExtendingBogus/FoodDataSet.cs new file mode 100644 index 00000000..01178be7 --- /dev/null +++ b/Examples/ExtendingBogus/FoodDataSet.cs @@ -0,0 +1,43 @@ +using Bogus; +using Bogus.Premium; + +namespace ExtendingBogus +{ + /// + /// The following shows how to create a dedicated DataSet accessible via C# extension method. + /// + public static class ExtensionsForCandy + { + public static Food Food(this Faker faker) + { + return ContextHelper.GetOrSet(faker, () => new Food()); + } + } + + /// + /// This DatSet can be created manually using `new Candy()`, or by fluent extension method via . + /// + public class Food : DataSet + { + private static readonly string[] Candies = + { + "Hard candy", "Taffy", "Chocolate bar", "Stick candy", + "Jelly bean", "Mint", "Cotton candy", "Lollipop" + }; + + /// + /// Returns some type of candy. + /// + 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); + } + } + +} diff --git a/Examples/ExtendingBogus/Program.cs b/Examples/ExtendingBogus/Program.cs new file mode 100644 index 00000000..63573a8b --- /dev/null +++ b/Examples/ExtendingBogus/Program.cs @@ -0,0 +1,27 @@ +using Bogus; + +namespace ExtendingBogus +{ + class Program + { + static void Main(string[] args) + { + var userFaker = new Faker() + //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; + } +} diff --git a/Examples/ExtendingBogus/README.md b/Examples/ExtendingBogus/README.md new file mode 100644 index 00000000..5e223ede --- /dev/null +++ b/Examples/ExtendingBogus/README.md @@ -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" +} +``` \ No newline at end of file diff --git a/README.md b/README.md index 029d4adf..64938d81 100644 --- a/README.md +++ b/README.md @@ -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) |