Class JsonSchemaGenerator
- Namespace
- Cratis.Chronicle.Schemas
- Assembly
- Cratis.Chronicle.dll
Represents an implementation of IJsonSchemaGenerator.
[Singleton]
public class JsonSchemaGenerator : IJsonSchemaGenerator
- Inheritance
-
JsonSchemaGenerator
- Implements
- Inherited Members
- Extension Methods
Constructors
JsonSchemaGenerator(IComplianceMetadataResolver, ISecurityMetadataResolver, INamingPolicy, IDerivedTypes?)
Initializes a new instance of the JsonSchemaGenerator class.
public JsonSchemaGenerator(IComplianceMetadataResolver metadataResolver, ISecurityMetadataResolver securityMetadataResolver, INamingPolicy namingPolicy, IDerivedTypes? derivedTypes = null)
Parameters
metadataResolverIComplianceMetadataResolverIComplianceMetadataResolver for resolving metadata.
securityMetadataResolverISecurityMetadataResolverISecurityMetadataResolver for resolving security metadata - required. Security metadata is never optional: a schema that carries an
[Encrypted]value is resolved through this exactly as unconditionally as a[PII]value is resolved throughmetadataResolver.namingPolicyINamingPolicyINamingPolicy to use for converting names during serialization.
derivedTypesIDerivedTypesIDerivedTypes used to recognize polymorphic base types adorned with DerivedTypeAttribute. Defaults to the derived types of the current type universe.
JsonSchemaGenerator(IComplianceMetadataResolver, INamingPolicy, IDerivedTypes?)
Initializes a new instance of the JsonSchemaGenerator class.
public JsonSchemaGenerator(IComplianceMetadataResolver metadataResolver, INamingPolicy namingPolicy, IDerivedTypes? derivedTypes = null)
Parameters
metadataResolverIComplianceMetadataResolverIComplianceMetadataResolver for resolving metadata.
namingPolicyINamingPolicyINamingPolicy to use for converting names during serialization.
derivedTypesIDerivedTypesIDerivedTypes used to recognize polymorphic base types adorned with DerivedTypeAttribute. Defaults to the derived types of the current type universe.
Remarks
Preserves the pre-[Encrypted] construction shape exactly - an instance built this way never
resolves or writes security metadata, because it has no provider to resolve it from. Use the overload
taking an ISecurityMetadataResolver for a generator that resolves both.
Methods
Generate(Type)
Generate a JsonSchema for a specific type.
public JsonSchema Generate(Type type)
Parameters
Returns
- JsonSchema
A generated JsonSchema.
Remarks
The schema for a type is generated once and then handed out for every subsequent request. Generating one walks the whole type graph reflectively - every property, its nullability, its compliance metadata and any derived types - which is work that cannot change while the process is running, because the answer is a function of the CLR type alone.
Without the memo this is recomputed per call, and the callers that matter call it per *instance* rather than per type: the read model compliance release pass asks for the schema once for every instance it releases, so serving a collection of a thousand read models generated the same schema a thousand times - measured at roughly a quarter of a second of pure waste for a read model of moderate size, on every emission of an observable query, for every subscriber.
Sharing one instance is consistent with how a JsonSchema is already treated everywhere else: the client's event-type schemas are cached and read concurrently on the same assumption, that a schema is effectively immutable once built. Its own derived answers are memoized internally with release semantics, so concurrent readers of a shared instance are safe.
Generate(Type, IEventTypes)
Generates a precise event schema using the owning store's event identities.
public JsonSchema Generate(Type type, IEventTypes eventTypes)
Parameters
typeTypeThe event type.
eventTypesIEventTypesThe owning store's finalized event registry.
Returns
- JsonSchema
The generated schema.
Exceptions
- RegistryAwareEventTypeSchemasNotSupported
The generator does not support a required explicit compensation binding.
GenerateForReadModel(Type)
Generate a JsonSchema for a read model type.
public JsonSchema GenerateForReadModel(Type type)
Parameters
Returns
- JsonSchema
A generated JsonSchema.
Remarks
Uses the same precise shape as Generate(Type).
GenerateLegacyEventType(Type)
Generates the legacy event schema for a kernel without precise schema support.
public JsonSchema GenerateLegacyEventType(Type type)
Parameters
typeTypeThe event type.
Returns
- JsonSchema
The legacy schema.
Remarks
Preserves the default-only shape used by kernels that do not advertise precise event type schemas. This representation is only used when registering against those kernels, not for read models.
Exceptions
- LegacyEventTypeSchemasNotSupported
The generator does not support legacy event schemas.
GenerateLegacyEventType(Type, IEventTypes)
Generates a legacy event schema using the owning store's event identities.
public JsonSchema GenerateLegacyEventType(Type type, IEventTypes eventTypes)
Parameters
typeTypeThe event type.
eventTypesIEventTypesThe owning store's finalized event registry.
Returns
- JsonSchema
The generated legacy schema.
Exceptions
- RegistryAwareEventTypeSchemasNotSupported
The generator does not support a required explicit compensation binding.