Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default MongoDB serialization of public properties

I understand that I must have public read and write properties on my class for MongoDB driver to serialize/deserialize my objects. however I want to know whether there is method/preferred method for hiding the write properties from the rest of my code?

eg.

class Product
{
    private List<Release> releases;

    public List<Release> Releases
    {
        get
        {
            return new List<Release>(releases); //I can protect 'releases' when reading by passing a copy of it
        }
        set
        {
            releases = value; //BUT how can I protect release when writing?
        }
    }
}

I want MongoDB to be able to serialize/deserialize my types but I don't want the rest of my code to be able to overwrite it's fields / properties that should otherwise have been private. Is there a pattern to handle this? I have thought about having a separate ProductDoc class which is just used as a intermediary for getting Product objects into and out of MongoDB, but I'm not sure whether there is a better solution to this.

like image 219
Daniel Robinson Avatar asked Sep 04 '12 12:09

Daniel Robinson


2 Answers

I have not worked with mongo for a long time for now. But you may try to read this thread MongoDb Map Setters or try to make your setter protected like this:

public List<Release> Releases
{
    get
    {
        return new List<Release>(releases); //I can protect 'releases' when reading by passing a copy of it
    }
    protected set
    {
        releases = value; //BUT how can I protect release when writing?
    }
}
like image 196
petro.sidlovskyy Avatar answered Sep 30 '22 15:09

petro.sidlovskyy


Another approach, if your properties are set by the constructor is to keep them read only and to use MapCreator to tell MongoDB how to create an instance of your class passing in the properties you want to set.

e.g. I have a class called Time with three readonly properties: Hour, Minute and Second and a public constructor that takes an hour, a minute and a second value.

Here's how I get MongoDB to store those three values in the database and to construct new Time objects during deserialization.

BsonClassMap.RegisterClassMap<Time>(cm =>
{
    cm.AutoMap();
    cm.MapCreator(p => new Time(p.Hour, p.Minute, p.Second));
    cm.MapProperty(p => p.Hour);
    cm.MapProperty(p => p.Minute);
    cm.MapProperty(p => p.Second);
}
like image 34
Ian Mercer Avatar answered Sep 30 '22 15:09

Ian Mercer