using ZB.MOM.WW.CBDD.Bson; using ZB.MOM.WW.CBDD.Bson.Schema; using ZB.MOM.WW.CBDD.Core.Indexing; namespace ZB.MOM.WW.CBDD.Core.Collections; /// /// Base class for custom mappers that provides bidirectional IndexKey mapping for standard types. /// public abstract class DocumentMapperBase : IDocumentMapper where T : class { /// /// Gets the target collection name for the mapped entity type. /// public abstract string CollectionName { get; } /// /// Serializes an entity instance into BSON. /// /// The entity to serialize. /// The BSON writer to write into. /// The number of bytes written. public abstract int Serialize(T entity, BsonSpanWriter writer); /// /// Deserializes an entity instance from BSON. /// /// The BSON reader to read from. /// The deserialized entity. public abstract T Deserialize(BsonSpanReader reader); /// /// Gets the identifier value from an entity. /// /// The entity to read the identifier from. /// The identifier value. public abstract TId GetId(T entity); /// /// Sets the identifier value on an entity. /// /// The entity to update. /// The identifier value to assign. public abstract void SetId(T entity, TId id); /// /// Converts a typed identifier value into an index key. /// /// The identifier value. /// The index key representation of the identifier. public virtual IndexKey ToIndexKey(TId id) { return IndexKey.Create(id); } /// /// Converts an index key back into a typed identifier value. /// /// The index key to convert. /// The typed identifier value. public virtual TId FromIndexKey(IndexKey key) { return key.As(); } /// /// Gets all mapped field keys used by this mapper. /// public virtual IEnumerable UsedKeys => GetSchema().GetAllKeys(); /// /// Builds the BSON schema for the mapped entity type. /// /// The generated BSON schema. public virtual BsonSchema GetSchema() { return BsonSchemaGenerator.FromType(); } } /// /// Base class for mappers using ObjectId as primary key. /// public abstract class ObjectIdMapperBase : DocumentMapperBase, IDocumentMapper where T : class { /// public override IndexKey ToIndexKey(ObjectId id) { return IndexKey.Create(id); } /// public override ObjectId FromIndexKey(IndexKey key) { return key.As(); } } /// /// Base class for mappers using Int32 as primary key. /// public abstract class Int32MapperBase : DocumentMapperBase where T : class { /// public override IndexKey ToIndexKey(int id) { return IndexKey.Create(id); } /// public override int FromIndexKey(IndexKey key) { return key.As(); } } /// /// Base class for mappers using String as primary key. /// public abstract class StringMapperBase : DocumentMapperBase where T : class { /// public override IndexKey ToIndexKey(string id) { return IndexKey.Create(id); } /// public override string FromIndexKey(IndexKey key) { return key.As(); } } /// /// Base class for mappers using Guid as primary key. /// public abstract class GuidMapperBase : DocumentMapperBase where T : class { /// public override IndexKey ToIndexKey(Guid id) { return IndexKey.Create(id); } /// public override Guid FromIndexKey(IndexKey key) { return key.As(); } }