Files
adas-core/adas-core.Infrastructure/Utils/MongoMaps/BoxConfigMapContributor.cs
T

56 lines
2.8 KiB
C#

using adas_core.Domain.Enums;
using adas_core.Domain.Models.BsonConverters;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using Stream = adas_core.Domain.Models.MongoModels.Stream;
namespace adas_core.Infrastructure.Utils.MongoMaps;
/// <summary>
/// Provides configuration mapping contributions for box entities, implementing the <see cref="IEntityMapContributor"/> interface to participate in entity map setup.
/// </summary>
/// <!-- aidoc:v1 sig=1b5af6b -->
public class BoxConfigMapContributor : IEntityMapContributor
{
/// <summary>
/// Registers the BSON class maps used to control how <see cref="Box"/> and <see cref="Sensor"/> instances are serialized to and from MongoDB. Each registration is only performed when no class map is already registered for the target type, and the mappings exclude properties that should not be persisted, rename persisted members where needed, apply <see cref="DictionaryBsonConverter"/> to the <c>Configuration</c> member of <see cref="Box"/>, and configure null/default-value handling for <see cref="Sensor"/> members.
/// </summary>
/// <!-- aidoc:v1 sig=d4dea7a body=eb04ab5 -->
public void RegisterMaps()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(Box)))
BsonClassMap.RegisterClassMap<Box>(cm =>
{
cm.AutoMap();
cm.UnmapMember(c => c.PointOfCare); //Esto hace que ignora la propiedad en la serialización del Bson
cm.MapMember(c => c.Bed).SetElementName("name");
cm.UnmapMember(c => c.IsActive);
cm.UnmapMember(c => c.IsVisible);
cm.MapMember(c => c.Configuration)
.SetSerializer(new DictionaryBsonConverter())
.SetIgnoreIfNull(true);
cm.UnmapMember(c => c.Location);
cm.UnmapMember(c => c.HasPatient);
cm.UnmapMember(c => c.Patientid);
cm.UnmapMember(c => c.Patient);
cm.UnmapMember(c => c.AttendingDoctor);
cm.UnmapMember(c => c.Type);
cm.UnmapMember(c => c.Observations);
cm.UnmapMember(c => c.Medication);
});
if (!BsonClassMap.IsClassMapRegistered(typeof(Sensor)))
BsonClassMap.RegisterClassMap<Sensor>(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.Name).SetIgnoreIfNull(true);
cm.MapMember(c => c.Title).SetIgnoreIfNull(true);
cm.MapMember(c => c.OnlyNumbers).SetDefaultValue(true);
cm.MapMember(c => c.IsGeneral).SetDefaultValue(true);
});
}
}