API Reference: Dependency Injection and Replayable Rolling

This reference documents the public types related to dependency injection registration and seeded/replayable die rolling for the d20Tek.DiceNotation library. For the core library types, see the main API Reference. The descriptions are derived from the library's XML documentation comments.

For a task-focused walkthrough of seeded and replayable rolling, see the Reproducible Rolling guide.

Table of Contents


Namespace: d20Tek.DiceNotation.DieRoller (Replayable Rolling)

These types provide reproducible (seeded) rolling as a capability across the seedable die rollers. The RandomDieRoller and MathNetDieRoller implementations (documented in the main API Reference) implement IReplayableDieRoller when constructed with a seed. MathNetDieRoller is in the separate d20tek-dicenotation-mathnet package; see Package: d20tek-dicenotation-mathnet.

IReplayableDieRoller

Defines a die roller that produces a reproducible sequence of rolls and can expose a ReplayToken describing how that sequence can be recreated. Inherits IDieRoller.

Properties

Member Description
ReplayToken? ReplayToken { get; } Gets the replay token capturing the algorithm and seed used to produce this roller's sequence, or null when the roller was created from an external generator whose seed is unknown.

DieRollerAlgorithm

Identifies the random number generation algorithm used by a replayable die roller. The algorithm is captured as part of a ReplayToken so that a reproducible roll sequence can be reconstructed with the same generator.

Values

Member Description
Random The standard System.Random pseudo-random generator.
MathNetMersenneTwister The Math.NET Numerics Mersenne Twister random source. Seeded rolling and replay need the d20tek-dicenotation-mathnet package and its MathNetSeededDieRollerProvider.

ReplayToken

Captures the information required to reproduce a seeded roll sequence: the generation algorithm and the seed value. A replay token can be persisted and later supplied to DieRollerFactory.CreateFromReplayToken to recreate an equivalent die roller. Reproducibility is only guaranteed for the same algorithm on a compatible platform and library version. Cryptographic rollers are not replayable, because their generators are not seedable.

Properties

Member Description
DieRollerAlgorithm Algorithm { get; init; } Gets the random generation algorithm used to produce the sequence.
int Seed { get; init; } Gets the seed value that initializes the generator.

ISeededDieRollerProvider

Creates seeded, replayable die rollers for one DieRollerAlgorithm, so that die rollers defined outside this library can take part in seeded rolling and replay through DieRollerFactory. Register providers with DiceNotationOptionsBuilder.AddSeededDieRollerProvider, or pass them to the DieRollerFactory overloads that take providers.

Members

Member Description
DieRollerAlgorithm Algorithm { get; } Gets the algorithm whose die rollers this provider creates.
IReplayableDieRoller Create(int seed, IAllowRollTrackerEntry? tracker = null) Creates a die roller for Algorithm that produces a reproducible sequence of rolls.

DieRollerFactory

Provides factory methods for creating replayable die rollers, including reconstructing a roller from a previously captured ReplayToken.

Static Methods

static IReplayableDieRoller CreateSeeded(int seed, DieRollerAlgorithm algorithm = DieRollerAlgorithm.Random, IAllowRollTrackerEntry? tracker = null)

Creates a seeded, replayable die roller that produces a reproducible sequence of rolls.

  • seed - The seed used to initialize the generator.
  • algorithm - The generation algorithm to use. Defaults to DieRollerAlgorithm.Random.
  • tracker - An optional tracker used to record roll entries.
  • Returns a replayable die roller initialized with the specified seed and algorithm.
  • Throws ArgumentOutOfRangeException when algorithm is not a supported value.

static IReplayableDieRoller CreateFromReplayToken(ReplayToken token, IAllowRollTrackerEntry? tracker = null)

Reconstructs a replayable die roller from a previously captured ReplayToken, producing the same sequence of rolls as the original seeded roller.

  • token - The replay token describing the algorithm and seed to reproduce.
  • tracker - An optional tracker used to record roll entries.
  • Returns a replayable die roller equivalent to the one that produced the token.
  • Throws ArgumentNullException when token is null.
  • Throws ArgumentOutOfRangeException when the token's algorithm is not supported for replay.

static IReplayableDieRoller CreateSeeded(int seed, DieRollerAlgorithm algorithm, IAllowRollTrackerEntry? tracker, IEnumerable<ISeededDieRollerProvider> providers)

static IReplayableDieRoller CreateFromReplayToken(ReplayToken token, IAllowRollTrackerEntry? tracker, IEnumerable<ISeededDieRollerProvider> providers)

These overloads work like the ones above, but use the supplied providers for algorithms that are not built into this library. Built-in algorithms are always created by the library itself. When several providers handle the same algorithm, the last one is used.

  • providers - The providers to search when the algorithm is not built in.
  • Throws ArgumentNullException when providers (or token) is null.
  • Throws ArgumentOutOfRangeException when the algorithm is neither built in nor handled by any of the providers.

Namespace: d20Tek.DiceNotation.DependencyInjection

This namespace provides integration with Microsoft.Extensions.DependencyInjection, allowing the dice notation services to be registered cleanly in an application's service container.

DiceServiceCollectionExtensions

Provides extension methods for registering d20Tek.DiceNotation services with an IServiceCollection.

Methods

static IServiceCollection AddDiceNotation(this IServiceCollection services, Action<DiceNotationOptionsBuilder> configure = null, ServiceLifetime lifetime = ServiceLifetime.Scoped)

Registers the dice notation services, including IDiceConfiguration, IDieRoller, and IDice, with the specified service collection.

  • services - The service collection to add the services to.
  • configure - An optional delegate used to configure dice behavior, the die roller, and roll tracking.
  • lifetime - The service lifetime used for the registered services. Defaults to ServiceLifetime.Scoped.
  • Returns the same service collection so that additional calls can be chained.
  • Throws ArgumentNullException when services is null.

DiceNotationOptionsBuilder

Provides a fluent builder for configuring the services registered by AddDiceNotation.

Methods

DiceNotationOptionsBuilder WithDefaultDieSides(int dieSides)

Sets the default number of die sides used when a notation omits an explicit side count.

DiceNotationOptionsBuilder WithBoundedResult(bool hasBoundedResult, int boundedMinimum = 1)

Configures whether roll results are bounded by a minimum value and sets that minimum.

DiceNotationOptionsBuilder UseRandomDieRoller()

Configures the default die roller to use the standard RandomDieRoller.

DiceNotationOptionsBuilder UseCryptoDieRoller()

Configures the default die roller to use the cryptographically secure CryptoDieRoller.

DiceNotationOptionsBuilder UseSeededDieRoller(int seed)

Configures the default die roller to use a seeded RandomDieRoller that produces a reproducible, replayable sequence of rolls.

DiceNotationOptionsBuilder UseReplayDieRoller(ReplayToken token)

Configures the default die roller to reproduce a previously captured roll sequence from a ReplayToken. Algorithms that are not built into this library are created by the registered ISeededDieRollerProvider services. Throws ArgumentNullException when token is null.

DiceNotationOptionsBuilder AddSeededDieRollerProvider(ISeededDieRollerProvider provider)

Registers a provider, as an ISeededDieRollerProvider singleton, that creates seeded die rollers for an algorithm that is not built into this library, so that UseReplayDieRoller can replay its tokens. Providers registered directly with the service collection are also used. Throws ArgumentNullException when provider is null.

DiceNotationOptionsBuilder UseConstantDieRoller(int rollValue = 1)

Configures the default die roller to use a ConstantDieRoller that always returns the specified value.

DiceNotationOptionsBuilder UseDieRoller(Func<IServiceProvider, IDieRoller> dieRollerFactory)

Configures the default die roller using a custom factory. Throws ArgumentNullException when dieRollerFactory is null.

DiceNotationOptionsBuilder UseRollTracker()

Registers a DieRollTracker as the roll tracker, exposed through IDieRollTracker and IAllowRollTrackerEntry.

DiceNotationOptionsBuilder UseAggregateRollTracker()

Registers an AggregateRollTracker as the roll tracker, exposed through IAggregateRollTracker and IAllowRollTrackerEntry.

DiceNotationOptionsBuilder UseRollTracker<TTracker, TService>()

Registers a custom roll tracker implementation, exposed through the specified tracker service type as well as IAllowRollTrackerEntry.


Package: d20tek-dicenotation-mathnet

The Math.NET based die roller lives in its own package, so applications that don't use it don't take on the MathNet.Numerics dependency. Install it with dotnet add package d20tek-dicenotation-mathnet. MathNetDieRoller itself is documented in the main API Reference.

MathNetSeededDieRollerProvider

Namespace d20Tek.DiceNotation.DieRoller. Implements ISeededDieRollerProvider for DieRollerAlgorithm.MathNetMersenneTwister, so DieRollerFactory and UseReplayDieRoller can reproduce Math.NET roll sequences.

Member Description
DieRollerAlgorithm Algorithm { get; } Returns DieRollerAlgorithm.MathNetMersenneTwister.
IReplayableDieRoller Create(int seed, IAllowRollTrackerEntry? tracker = null) Creates a seeded MathNetDieRoller.

MathNetOptionsBuilderExtensions

Namespace Microsoft.Extensions.DependencyInjection, the same as AddDiceNotation, so code that configures the builder needs no extra using. Each method throws ArgumentNullException when builder is null and returns the same builder for chaining.

static DiceNotationOptionsBuilder UseMathNetDieRoller(this DiceNotationOptionsBuilder builder)

Configures the default die roller to use the Math.NET based MathNetDieRoller.

static DiceNotationOptionsBuilder UseSeededMathNetDieRoller(this DiceNotationOptionsBuilder builder, int seed)

Configures the default die roller to use a seeded MathNetDieRoller that produces a reproducible, replayable sequence of rolls.

static DiceNotationOptionsBuilder AddMathNetSeededDieRollerProvider(this DiceNotationOptionsBuilder builder)

Registers a MathNetSeededDieRollerProvider, so that UseReplayDieRoller can replay DieRollerAlgorithm.MathNetMersenneTwister tokens.

An unhandled error has occurred. Reload 🗙