By default MongoDB serialization of public objects

I understand that I must have public read and write properties in my class for the MongoDB driver to serialize / deserialize my objects. however, I want to know if there is a method / preferred method for hiding 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 overwrite its fields / properties, which would otherwise be private. Is there any way to handle this? I was thinking about having a separate ProductDoc class that is only used as an intermediary for getting Product objects in and out of MongoDB, but I'm not sure if there is a better solution for this.

+4
source share
3 answers

I have not worked with mongo for a long time. But you can try to read this Mapo Seto MongoDb chain or try to make your setter 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? } } 
+5
source

Another approach, if your properties are set by the constructor, is to save them read-only and use MapCreator to tell MongoDB how to instantiate your class passing in the properties you want to set.

eg. 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.

This is how I get MongoDB to store these three values ​​in a database and to create 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); } 
+2
source

The best answer on this page currently has some flaws that are important to understanding.

As a solution, it is currently written:

 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? } } 

It does not work as it is written.

 obj.Releases.Add(new Release()); 

Would affect the base collection by adding a new version to it. . This directly contradicts the stated goal of making the standard procedure private.

However, if you change the public type of the property to implement IEnumerable instead of List and return a version of the ReadOnly list. Such as...

 public IEnumerable<Release> Releases { get { return new List<Release>(releases).AsReadOnly(); } protected set { releases = value; } } 

Then both

 obj.Releases.Add(new Release()); 

and

 obj.Releases = new List<Release>(); 

will generate build errors and prevent the base collection from being modified.

0
source

Source: https://habr.com/ru/post/1432264/


All Articles