Configuration
Each value object can have its own optional configuration. Examples include:
The underlying type
Any 'conversions' (Dapper, System.Text.Json, Newtonsoft.Json, etc.) - see Integrations in the wiki for more information
The type of the exception that is thrown when validation fails
Default values are inferred from global configuration. Global configuration is specified as an assembly level attribute. Here's an example:
Most values are present in both ValueObject
and VogenDefaults
. The parameters for the ValueObject
attribute are:
underlyingType
—the type of the primitive that is being wrapped—defaults toint
conversions
- specified what conversion code is generated - defaults toConversions.Default
which generates type converters and a converter to handle serialization using System.Text.Jsonthrows
= specifies the type of exception thrown when validation fails—defaults toValueObjectValidationException
customizations
—simple customization switches—defaults toCustomizations.None
,deserializationStrictness
- specifies how strict deserialization is, e.g. should yourValidate
method be called, or should pre-defined instances that otherwise invalid be allowed - defaults toDeserializationStrictness.AllowValidAndKnownInstances
debuggerAttributes
- specifies the level that debug attributes are written as some IDEs don't support all of them, e.g., Rider - defaults toDebuggerAttributeGeneration.Full
which generatesDebuggerDisplay
and a debugger proxy type for IDEs that support themcomparison
—species which comparison code is generated—defaults toComparisonGeneration.UseUnderlying
which hoists anyIComparable
implementations from the primitivestringComparers
- specifies which string comparison code is generated—defaults toStringComparersGeneration.Omit
, which doesn't generate anything related to string comparisontoPrimitiveCasting
- specifies the type of casting from wrapper to primitive - defaults toCastOperator.Explicit
,fromPrimitiveCasting
- specifies the type of casting from primitive to wrapper - default toCastOperator.Explicit
parsableForStrings
- specifies what is generated forIParsable
types for strings - defaults toParsableForStrings.GenerateMethodsAndInterface
parsableForPrimitives
- specifies what is generated forParse
andTryParse
methods - defaults toParsableForPrimitives.HoistMethodsAndInterfaces
tryFromGeneration
- specifies what to write forTryFrom
methods—defaults toTryFromGeneration.GenerateBoolAndErrorOrMethods
isInitializedMethodGeneration
- specifies whether to generate anIsInitialized()
method - defaults toIsInitializedMethodGeneration.Generate
primitiveEqualityGeneration
- specified whether to generate primitive comparison operators.
The values that are specified only to global configuration are:
systemTextJsonConverterFactoryGeneration
- determines whether to write a factory for STJ converters which are useful as a replacement to Reflect in AOT/trimmed scenarios—defaults toSystemTextJsonConverterFactoryGeneration.Generate
staticAbstractsGeneration
- determines whether to write static abstract code - defaults toStaticAbstractsGeneration.Omit
openApiSchemaCustomizations
- determines what is generated to assist in OpenAPI scenarios, for instance, generate a schema filter for Swashbuckle, or generate an extension method withMapType
calls - defaults toOpenApiSchemaCustomizations.Omit
explicitlySpecifyTypeInValueObject
- specifies whether individual value objects should explicitly define the primitive type that they wrap - defaults tofalse
disableStackTraceRecordingInDebug
- disables stack trace recording; in Debug buids, a stack trace is recorded and is thrown in the exception when something is created in an uninitialized state, e.g. after deserialization
Several code analysis warnings exist for invalid configuration, including:
when you specify an exception that does not derive from
System.Exception
when your exception does not have one public constructor that takes an int
when the combination of conversions does not match an entry