https://github.com/dimitrietataru/ace-csharp-data-faker
C# data faker
https://github.com/dimitrietataru/ace-csharp-data-faker
bogus csharp data-factory data-faker dot-net
Last synced: 5 months ago
JSON representation
C# data faker
- Host: GitHub
- URL: https://github.com/dimitrietataru/ace-csharp-data-faker
- Owner: dimitrietataru
- License: mit
- Created: 2022-06-16T12:34:28.000Z (about 4 years ago)
- Default Branch: ace
- Last Pushed: 2025-04-14T03:35:21.000Z (about 1 year ago)
- Last Synced: 2025-05-15T02:36:14.049Z (about 1 year ago)
- Topics: bogus, csharp, data-factory, data-faker, dot-net
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# C# data faker
[](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/build.yml)
[](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/release.yml)
[](https://www.nuget.org/packages/AceCSharp.DataFaker)
### What is AceCSharp.DataFaker
It's a library, that wraps around Bogus, built to easily generate fake data using a syntactic sugar syntax (_Fake.Of\()_).
To configure how the Foo type is generated, add a new __private static property__ named _FakeFoo_. Relies on __duck-typing__ and uses _Reflection_ to find and use the appropriate configuration.
The Bogus complex configuration is seggregated from its usage.
### Setup
``` csharp
public class Foo
{
public Guid Id { get; set; }
public string Title { get; set; }
public int Index { get; set; }
public bool IsActive { get; set; }
}
public class FakeDto
{
private static Faker FakeFoo =>
new Faker()
.RuleFor(
foo => foo.Id,
func => func.Random.Uuid())
.RuleFor(
foo => foo.Title,
func => func.Lorem.Sentence())
.RuleFor(
foo => foo.Index,
func => func.Random.Int(min: 1, max: int.MaxValue))
.RuleFor(
foo => foo.IsActive,
func => func.Random.Bool())
.StrictMode(ensureRulesForAllProperties: true);
}
```
### Usage
``` csharp
var foo = Fake.Of();
var threeFoos = Fake.ManyOf(count: 3);
var manyFoos = Fake.ManyOf(minCount: 10, maxCount: 100);
```
---
### Extend your container
``` csharp
public class FakeDto
{
// ...
public static T Of()
where T : class
{
return Fake.Of();
}
public static List ManyOf(int count = 3)
where T : class
{
return Fake.ManyOf(count);
}
public static List ManyOf(int minCount, int maxCount)
where T : class
{
return Fake.ManyOf(minCount, maxCount);
}
// ...
}
```
### Usage
``` csharp
var foo = FakeDto.Of();
var threeFoos = FakeDto.ManyOf(count: 3);
var manyFoos = FakeDto.ManyOf(minCount: 10, maxCount: 100);
```
---
### What if I don't have a configuration?
Use one of the `..OrDefault` methods. It will try to find the _FakeBar_ configuration, otherwise return the default _Faker\_ instance.
* OfOrDefault
* ManyOfOrDefault
``` csharp
// When you did not configure the FakeDto class
var bar = Fake.Of();
// When you did not configure the FakeBar property
var bar = Fake.OfOrDefault();
```
---
### See also
* [Bogus](https://github.com/bchavez/Bogus)
### License
AceCSharp.DataFaker is Copyright © 2022 [Dimitrie Tataru](https://github.com/dimitrietataru) and other contributors under the [MIT license](https://github.com/dimitrietataru/ace-csharp-data-faker/blob/ace/LICENSE.txt).