Skip to content

Cratis Fundamentals value generation

Cratis Fundamentals lets a C# concept declare how to create its own values through a compile-time contract. Use it when handlers and factories need the same rule for creating an identifier, code or token. The caller asks for a value without choosing the generation strategy again at every call site.

A concept is a type that wraps a value with domain meaning, such as CustomerId instead of a raw Guid. Its New() method chooses how to generate that value. Production code can call it when needed, while a spec can supply a known value and test the behavior without depending on random output.

The caller decides that

The concept declares how

pins instead of generating

IGeneratable<T>
static T New()

IGeneratableInContext<T>
static T New(GenerationContext)

GenerateValue
Uuid, UuidV5, UuidV7, Ulid, TypeId, NanoId, Bytes, Pattern, Integer, Sha256

Your code
T.New()

A framework
for example Arc [GeneratedValue]

A spec
new CustomerId(known value)

Implement IGeneratable<TSelf> when the concept can generate a value without operation-specific input. Generic code constrained to T : IGeneratable<T> can call T.New() without reflection and receive the concept itself. Implement IGeneratableInContext<TSelf> when generation needs the operation’s captured time or tenant.

GenerateValue supplies the strategies used inside New(). Its random strategies draw from a cryptographically secure random number generator; UuidV5 and Sha256 derive deterministic results from supplied input. A fixed GenerationContext fixes the time used by time-bearing strategies, but leaves their random part random.

Start with the value generation tutorial to build shop identifiers, invitation codes and an API key. You will call the concepts from generic code, then write a spec that fixes the time in an order identifier. The guides below help you choose a strategy for your own values and handle the result safely.

Your situation Read
You want to see it working, start to finish Tutorial: generate your first values
You know what you need and want the right strategy Choosing a strategy
A value must follow the operation’s time or tenant Context-dependent values
A readable reference needs a format, UTC date or check digits Patterns and checksums
You are creating tokens, links, API keys or key material Secrets and tokens
You need a deterministic fingerprint or a high-entropy token verifier SHA-256 digests
Your specs break because ids are random Testing generated values
You need exact signatures, limits and encodings Reference

These APIs are in Cratis.Fundamentals and target .NET 8, 9 and 10. Concepts themselves are introduced on the Concepts page.