4

I have many immutable value type classes, for example EmailAddress, which ensure any non null instance is valid. I would like to control the serialization of these types of objects to be just the standard string representation ("123@abc.com") when persisted using MongoDB C# Driver.

I have tried implementing the IBsonSerilizer however it will only allow for objects or arrays at the root level. I was able to implement proper Json Serilization with Json.NET, is there a different approach I should be taking?

Steven Carlson
  • 925
  • 1
  • 10
  • 25
Gent
  • 2,675
  • 1
  • 24
  • 34

3 Answers3

6

I assume you mean an EmailAddress class something like this:

[BsonSerializer(typeof(EmailAddressSerializer))]
public class EmailAddress
{
    private string _value;

    public EmailAddress(string value)
    {
        _value = value;
    }

    public string Value
    {
        get { return _value; }
    }
}

I've used an attribute to link the EmailAddress class to a custom serializer, which could be implemented like this:

public class EmailAddressSerializer : BsonBaseSerializer
{
    public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        if (bsonReader.GetCurrentBsonType() == BsonType.Null)
        {
            bsonReader.ReadNull();
            return null;
        }
        else
        {
            var value = bsonReader.ReadString();
            return new EmailAddress(value);
        }
    }

    public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        if (value == null)
        {
            bsonWriter.WriteNull();
        }
        else
        {
            var emailAddress = (EmailAddress)value;
            bsonWriter.WriteString(emailAddress.Value);
        }
    }
}

You can't serialize an EmailAddress as the root document (because it's not a document...). But you could use an EmailAddress embedded in some other document. For example:

public class Person
{
    public int Id { get; set; }
    public EmailAddress EmailAddress { get; set; }
}

Which you could test using code like the following:

var person = new Person { Id = 1, EmailAddress = new EmailAddress("joe@xyz.com") };
var json = person.ToJson();
var rehyrdated = BsonSerializer.Deserialize<Person>(json);

The resulting JSON/BSON document is:

{ "_id" : 1, "EmailAddress" : "joe@xyz.com" }
Robert Stam
  • 12,039
  • 2
  • 39
  • 36
  • 1
    Thank you for taking the time to set me straight. Your right, in my unit test I was trying to serialize it directly which is why I ran into this error, I have adjusted the test to serialize a parent object with a property of this type. – Gent May 12 '13 at 19:07
  • This works to serialize, but how to search for a Person with specific email using Linq? Mongo throws exception on the "Value" property because it does not exist. – Marcos Junior Jan 05 '18 at 10:50
3

After I read the answer of @Davide Icardi I found out there is built in convention to use immutable objects.

Just register the convention

ConventionRegistry.Register(nameof(ImmutableTypeClassMapConvention), 
new ConventionPack { new ImmutableTypeClassMapConvention()}, type => true);
alsami
  • 8,996
  • 3
  • 25
  • 36
  • 2
    The difference is that the ImmutableTypeClassMapConvention only works if you have all read-only-propeties actually present in the constructor. It will not work if you have any properties that are not present as constructor parameters, for example the "FullName" property in Davide's example – adhominem Mar 25 '20 at 10:25
2

I have tried to solve this problem by creating a convention that map all read only properties that match a constructor and also the matched constructor.

Assume that you have a class like:

public class Person
{
    public string FirstName { get; }
    public string LastName { get; }
    public string FullName => FirstName + LastName;
    public ImmutablePocoSample(string lastName)
    {
        LastName = lastName;
    }

    public ImmutablePocoSample(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

Here is the code of the convention:

/// <summary>
/// A convention that map all read only properties for which a matching constructor is found.
/// Also matching constructors are mapped.
/// </summary>
public class ImmutablePocoConvention : ConventionBase, IClassMapConvention
{
    private readonly BindingFlags _bindingFlags;

    public ImmutablePocoConvention()
            : this(BindingFlags.Instance | BindingFlags.Public)
    { }

    public ImmutablePocoConvention(BindingFlags bindingFlags)
    {
        _bindingFlags = bindingFlags | BindingFlags.DeclaredOnly;
    }

    public void Apply(BsonClassMap classMap)
    {
        var readOnlyProperties = classMap.ClassType.GetTypeInfo()
            .GetProperties(_bindingFlags)
            .Where(p => IsReadOnlyProperty(classMap, p))
            .ToList();

        foreach (var constructor in classMap.ClassType.GetConstructors())
        {
            // If we found a matching constructor then we map it and all the readonly properties
            var matchProperties = GetMatchingProperties(constructor, readOnlyProperties);
            if (matchProperties.Any())
            {
                // Map constructor
                classMap.MapConstructor(constructor);

                // Map properties
                foreach (var p in matchProperties)
                    classMap.MapMember(p);
            }
        }
    }

    private static List<PropertyInfo> GetMatchingProperties(ConstructorInfo constructor, List<PropertyInfo> properties)
    {
        var matchProperties = new List<PropertyInfo>();

        var ctorParameters = constructor.GetParameters();
        foreach (var ctorParameter in ctorParameters)
        {
            var matchProperty = properties.FirstOrDefault(p => ParameterMatchProperty(ctorParameter, p));
            if (matchProperty == null)
                return new List<PropertyInfo>();

            matchProperties.Add(matchProperty);
        }

        return matchProperties;
    }


    private static bool ParameterMatchProperty(ParameterInfo parameter, PropertyInfo property)
    {
        return string.Equals(property.Name, parameter.Name, System.StringComparison.InvariantCultureIgnoreCase)
               && parameter.ParameterType == property.PropertyType;
    }

    private static bool IsReadOnlyProperty(BsonClassMap classMap, PropertyInfo propertyInfo)
    {
        // we can't read 
        if (!propertyInfo.CanRead)
            return false;

        // we can write (already handled by the default convention...)
        if (propertyInfo.CanWrite)
            return false;

        // skip indexers
        if (propertyInfo.GetIndexParameters().Length != 0)
            return false;

        // skip overridden properties (they are already included by the base class)
        var getMethodInfo = propertyInfo.GetMethod;
        if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != classMap.ClassType)
            return false;

        return true;
    }
}

You can register i using:

ConventionRegistry.Register(
    nameof(ImmutablePocoConvention),
    new ConventionPack { new ImmutablePocoConvention() },
    _ => true);
Davide Icardi
  • 11,919
  • 8
  • 56
  • 77