Overview
The ParamsSystem is a small contract layer made of three types:
ParamKey<T>is a typed, named key. Its generic argument declares the type of value it holds.IParamKeyis the untyped interface that everyParamKey<T>implements. It exposes only the key's name.Paramsis the container. It maps keys to values.
The namespace for ParamKey<T> and IParamKey is SaffronLibrary.Prime.Systems.Param.Interfaces. The namespace for Params is SaffronLibrary.Prime.Systems.Param.
Design patterns used
- Typed key with an untyped storage key.
ParamKey<T>carries the value type at compile time.Paramsstores entries underIParamKey, because a generic key type cannot be used as a single dictionary key type. The untyped interface lets keys of different value types share one dictionary. - Generic accessors.
Set<T>,Get<T>, andTryGet<T>take aParamKey<T>, so the compiler ensures the value type matches the key type. - Static guard helpers.
Require,RequireObject, and theRequireObjectoverload forParamKey<object>are static methods that check presence and type, then throwInvalidOperationExceptionwith a message naming the key.
Key concepts
- Key name. A unique string, exposed as
KeyName. The name is the identity used whenParamsis flattened withToDictionary. - Key identity in storage.
ParamKey<T>does not override equality, so storage inParamscompares keys by reference. Two separate key instances are different entries even if they share a name. - Set and replace.
Setstores a value under a key. Setting the same key again replaces the earlier value. - Presence checks.
Hasreports whether a key has a value.TryGetreturns a value when it exists and reports success through its return value.
Data flow
- A domain defines
ParamKey<T>instances, usually as static readonly fields in a keys class. - A caller creates an empty
Paramsand fills it withSet. - The populated
Paramsis carried by the message that uses it. - A receiver reads values with
Get,TryGet,Has, or one of theRequirehelpers.
Error behavior
GetthrowsKeyNotFoundExceptionwhen no value is stored under the key.RequireandRequireObjectthrowInvalidOperationExceptionwhen the key is missing or the stored value is not of the expected type. TheParamKey<object>overload ofRequireObjectalso throwsArgumentNullExceptionfor null arguments.TryGetandHasdo not throw for a missing key.