How can I manage data structure changes in MongoDb collections using Simple.Data?

We are currently using Simple.Data and the MongoDb adapter. When we extracted the document, we pass it to POCO, for example:

(User)db.Users.FindById(1234);

To begin with, this works pretty well (hell, no circuit!). However, if we change the structure of the User object (for example, add a new field or change the data type of the field), we will no longer discard the original document, since it does not correspond to our new class structure.

To solve this problem, we have so far tried two of the simplest approaches:

  • Manual data updates to reflect changes in the structure of the document. Good at the moment, but not manageable when the project is deployed in several environments / makes it into production.
  • Manual matching eg. casting SimpleRecord into a dictionary and manually evaluating members. I am concerned about the implementation of this approach, although I have not yet noted it. I am also concerned that I did not find a way to make it common without using reflection in the type of destination to identify the names of the participants.

We also looked at ways to solve this problem using Ruby and Python . The former are addressed more often (saving versions of the old scheme in Min seems that this may be redundant).

Before I run away and spoil something crazy, did anyone solve this problem with Simple.Data? Can anyone offer any recommendations regarding best practices for dealing with document structure changes in inconclusive databases?

+3
1

. :

public class FieldsWrapper : IBsonSerializable
{
    public List<DataFieldValue> DataFieldValues { get; set; }


    public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
    {
    if (nominalType != typeof(FieldsWrapper)) throw new ArgumentException("Cannot deserialize anything but self");
    var doc = BsonDocument.ReadFrom(bsonReader);
    var list = new List<DataFieldValue>();
    foreach (var name in doc.Names)
    {
        var val = doc[name];
        if (val.IsString)
            list.Add(new DataFieldValue {LocalIdentifier = name, Values = new List<string> {val.AsString}});
        else if (val.IsBsonArray)
        {
            DataFieldValue df = new DataFieldValue {LocalIdentifier = name};
            foreach (var elem in val.AsBsonArray)
            {
                df.Values.Add(elem.AsString);
            }
            list.Add(df);
        }
    }
    return new FieldsWrapper {DataFieldValues = list};
    }


    public void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
    {
        if (nominalType != typeof (FieldsWrapper))
            throw new ArgumentException("Cannot serialize anything but self");
        bsonWriter.WriteStartDocument();
        foreach (var dataFieldValue in DataFieldValues)
        {

            bsonWriter.WriteName(dataFieldValue.LocalIdentifier);
            if (dataFieldValue.Values.Count != 1)
            {
                var list = new string[dataFieldValue.Values.Count];
                for (int i = 0; i < dataFieldValue.Values.Count; i++)
                    list[i] = dataFieldValue.Values[i];
                BsonSerializer.Serialize(bsonWriter, list); 
            }
            else
            {
                BsonSerializer.Serialize(bsonWriter, dataFieldValue.Values[0]); 
            }
        }
        bsonWriter.WriteEndDocument();
    }

}

, , -, . , , , , , .

+2

All Articles