Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectFakerExtensions

Namespace: AdCodicem.ValueObjects.Bogus
Assembly: AdCodicem.ValueObjects.Bogus.dll

Draws value objects from Bogus: a rule for every value-object member of a , and a value object drawn from a , each from the rules its type declares.

public static class ValueObjectFakerExtensions

Inheritance​

object ← ValueObjectFakerExtensions

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

Without a rule, Bogus leaves a value-object member at its default, the instance no rule of the type accepted; a rule written by hand restates the bounds the type declares, and drifts from them. Here each value is drawn by a : a candidate built from the type's schema, kept only once its rules accept it, or the example it declares.

Each value object takes one number from the faker's , which seeds the sampler that draws it: UseSeed replays the same values, and the rules after it see the same numbers whatever the sampler made of its own draws. A value object is found through ValueObjectRegistry.TryResolve, which describes a value object written by hand the first time it is asked for, and keeps that description.

Methods​

RuleForValueObjects<T>(Faker<T>, ValueObjectSamplerOptions?)​

Gives every value-object member of T a rule, its nullable, array and members included, each value drawn from the rules its type declares, so that StrictMode(true) is met.

public static Faker<T> RuleForValueObjects<T>(this Faker<T> faker, ValueObjectSamplerOptions? options = null) where T : class

Parameters​

faker Faker<T>

The faker to add the rules to.

options ValueObjectSamplerOptions?

How each value is drawn, or null for the defaults.

Returns​

Faker<T>

The faker, so that rules chain.

Type Parameters​

T

The type the faker builds.

Remarks​

Call it before the rules of the members it should leave alone: a rule of Bogus replaces the earlier rule of the same member, so a rule written after this one wins, and one written before is replaced. A nullable member gets a value, never null; an array or a list gets one to three values.

The rules are added through Bogus's RuleForType, so they reach exactly the members the faker binds, through a binder of its own included. A is thrown by Generate, when a member is drawn.

Exceptions​

ArgumentNullException

faker is null.

ValueObject<TSelf>(Faker, ValueObjectSamplerOptions?)​

Draws a value of a value object its rules accept, from the schema it declares.

public static TSelf ValueObject<TSelf>(this Faker faker, ValueObjectSamplerOptions? options = null) where TSelf : struct, IValueObject

Parameters​

faker Faker

The faker whose seeds the draw.

options ValueObjectSamplerOptions?

How the value is drawn, or null for the defaults.

Returns​

TSelf

The value object.

Type Parameters​

TSelf

The value object.

Remarks​

The underlying type is not named, since C# cannot infer it: the value object is closed through its descriptor. For a rule no schema carries, a checksum for one, name it and give a generator: .

Exceptions​

ArgumentNullException

faker is null.

ArgumentException

TSelf carries the marker of a value object but implements no IValueObject<TSelf, TValue> over itself.

ValueObjectSamplingException

No value the type accepts could be drawn.

ValueObject<TSelf, TValue>(Faker, Func<Faker, TValue>)​

Draws a value of a value object from a generator of its underlying value, a semantic one of Bogus's for instance, kept only once the type's rules accept it.

public static TSelf ValueObject<TSelf, TValue>(this Faker faker, Func<Faker, TValue> generator) where TSelf : struct, IValueObject<TSelf, TValue>

Parameters​

faker Faker

The faker handed to generator.

generator Func<Faker, TValue>

Draws a candidate underlying value: f => f.Finance.Iban().

Returns​

TSelf

The value object.

Type Parameters​

TSelf

The value object.

TValue

Its underlying type.

Remarks​

The generator is tried as many times as the sampler tries candidates, 100 by default, and the example the type declares comes out when none passed.

Exceptions​

ArgumentNullException

faker or generator is null.

ValueObjectSamplingException

None of the candidates the generator gave passed the type's rules, and it declares no example they accept.