dahomey-technologies/Dahomey.Cbor

[Question] Usage of objectMapping.SetOrderBy()

Closed this issue · 2 comments

First, thanks for implementing this!

I am trying to use the objectMapping.SetOrderBy() Method to get everything sorted alphabetically (including the discriminator), but unfortunately I can't apply the solution you provided right here #46 as I am using it this way:

on inititalization:

options.Registry.ObjectMappingConventionRegistry.RegisterProvider(new DsonObjectMappingConventionProvider(mode));

with:

public class DsonObjectMappingConventionProvider : IObjectMappingConventionProvider
    {
        private readonly IObjectMappingConvention _objectMappingConvention;

        public DsonObjectMappingConventionProvider(OutputMode mode)
        {
            _objectMappingConvention = new DsonObjectMappingConvention(mode);
        }

        public IObjectMappingConvention GetConvention(Type type)
        {
            return _objectMappingConvention;
        }
    }

and:

public class DsonObjectMappingConvention : IObjectMappingConvention
    {
       ....
       public void Apply<T>(SerializationRegistry registry, ObjectMapping<T> objectMapping) where T : class
       {
             ....
             objectMapping.SetOrderBy(m => m.MemberName);
       }
    }

I think the problem is that at the time where Apply(...) is executed the MemberNames are all null and cannot be sorted? What am I doing wrong?

I reproduced the problem as a bug