Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: about 2 months ago
JSON representation

C# data faker

Awesome Lists containing this project

README

        

# C# data faker

[![build](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/build.yml/badge.svg)](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/build.yml)
[![release](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/release.yml/badge.svg)](https://github.com/dimitrietataru/ace-csharp-data-faker/actions/workflows/release.yml)
[![NuGet](https://img.shields.io/nuget/v/AceCSharp.DataFaker)](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).