Home > Software engineering >  C# mongodb with strongly typed ID using records
C# mongodb with strongly typed ID using records

Time:03-21

I'm playing with strongly typed IDs using C#9 records with Mongo. I've successfuly managed to make inserting work but reading is throwing an exception (see below).

These are the model definitions. Pretty simple, TestModel has a TestModelId typed as a StronglyTypedId<Guid>.

public record TestModel(TestModelId Id, string value, string? value1);
public record TestModelId(Guid Id) : StronglyTypedId<Guid>(Id);
public abstract record StronglyTypedId<TValue>(TValue Id) where TValue : notnull;

These require a registration of a serialization provider and a serializer class.

Registration:

var guidSerializer = BsonSerializer.SerializerRegistry.GetSerializer<Guid>();
BsonSerializer.RegisterSerializationProvider(new IdSerializationProvider(guidSerializer));

Serializers:

public class IdSerializationProvider : IBsonSerializationProvider
{
    private IBsonSerializer<Guid> guidSerializer;

    public IdSerializationProvider(IBsonSerializer<Guid> guidSerializer)
    {
        this.guidSerializer = guidSerializer;
    }

    public IBsonSerializer? GetSerializer(Type type)
    {
        if(type.BaseType != null && type.BaseType == typeof(StronglyTypedId<Guid>)) 
            return new TestModelSerializer(type, guidSerializer);
        
        return null;
    }
}

public class TestModelSerializer : SerializerBase<StronglyTypedId<Guid>>
{
    private readonly Type targetType;
    private readonly IBsonSerializer<Guid> guidSerializer;

    public TestModelSerializer(Type targetType, IBsonSerializer<Guid> guidSerializer)
    {
        this.targetType = targetType;
        this.guidSerializer = guidSerializer;
    }

    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, StronglyTypedId<Guid> value)
    {
        guidSerializer.Serialize(context, args, value.Id);
    }

    public override StronglyTypedId<Guid> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        var guid = guidSerializer.Deserialize(context, args);
        var idType = Activator.CreateInstance(targetType, new object[] { guid });
        return (idType as StronglyTypedId<Guid>)!;
    }
}

The code below for inserting a new document works.

MongoClient dbClient = new MongoClient("mongodb://localhost:27017");
IMongoCollection<TestModel> collection = dbClient.GetDatabase("test").GetCollection<TestModel>("testmodel");

TestModel model = new TestModel(new TestModelId(Guid.NewGuid()), "test", "test");
collection.InsertOne(model);

But reading using Find is throwing an exception: System.InvalidOperationException: 'The operands for operator 'Equal' do not match the parameters of method 'op_Equality'.'

TestModel found = collection.Find(x => x.Id == model.Id).FirstOrDefault();

I tried using Equals instead, like this

TestModel found = collection.Find(x => x.Id.Equals(model.Id)).FirstOrDefault();

But this is throwing another exception: System.ArgumentException: 'Method 'Boolean Equals(WebApplication2.Controllers.TestModelId)' declared on type 'WebApplication2.Controllers.TestModelId' cannot be called with instance of type 'WebApplication2.Controllers.StronglyTypedId`1[System.Guid]''

I'm not sure what's happening there, the type definitions are pretty clear but for some reason there seems to be a type mismatch? I tried to google any solution to this but couldn't find anything.

CodePudding user response:

I did not find what caused the exception but I found a workaround:

var filter = Builders<TestModel>.Filter.Eq("_id", model.Id.Id);
TestModel found = collection.Find(filter).FirstOrDefault();
  • Related