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 tointconversions- specified what conversion code is generated - defaults toConversions.Defaultwhich generates type converters and a converter to handle serialization using System.Text.Jsonthrows= specifies the type of exception thrown when validation fails—defaults toValueObjectValidationExceptioncustomizations—simple customization switches—defaults toCustomizations.None,deserializationStrictness- specifies how strict deserialization is, e.g. should yourValidatemethod be called, or should pre-defined instances that otherwise invalid be allowed - defaults toDeserializationStrictness.AllowValidAndKnownInstancesdebuggerAttributes- specifies the level that debug attributes are written as some IDEs don't support all of them, e.g., Rider - defaults toDebuggerAttributeGeneration.Fullwhich generatesDebuggerDisplayand a debugger proxy type for IDEs that support themcomparison—species which comparison code is generated—defaults toComparisonGeneration.UseUnderlyingwhich hoists anyIComparableimplementations 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.ExplicitparsableForStrings- specifies what is generated forIParsabletypes for strings - defaults toParsableForStrings.GenerateMethodsAndInterfaceparsableForPrimitives- specifies what is generated forParseandTryParsemethods - defaults toParsableForPrimitives.HoistMethodsAndInterfacestryFromGeneration- specifies what to write forTryFrommethods—defaults toTryFromGeneration.GenerateBoolAndErrorOrMethodsisInitializedMethodGeneration- specifies whether to generate anIsInitialized()method - defaults toIsInitializedMethodGeneration.GenerateprimitiveEqualityGeneration- 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.GeneratestaticAbstractsGeneration- determines whether to write static abstract code - defaults toStaticAbstractsGeneration.OmitopenApiSchemaCustomizations- determines what is generated to assist in OpenAPI scenarios, for instance, generate a schema filter for Swashbuckle, or generate an extension method withMapTypecalls - defaults toOpenApiSchemaCustomizations.OmitexplicitlySpecifyTypeInValueObject- specifies whether individual value objects should explicitly define the primitive type that they wrap - defaults tofalsedisableStackTraceRecordingInDebug- 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.Exceptionwhen your exception does not have one public constructor that takes an int
when the combination of conversions does not match an entry