BsonElement attribute and custom deserialization logic with MongoDB C# driver -
consider following example:
public class foo { private string _text; [bsonelement("text"), bsonrequired] public string text { { return _text; } set { _text = value; bar(_text); } } private void bar(string text) { //only relevant when text set user of class, //not during deserialization } }
the setter of text
property and, subsequently, method bar
called both when user of class assigns new value property , during object deserialization mongodb c# driver. need ensure bar
called when text
property set user , not during deserialization.
i see 2 solutions don't suit me:
the first move bsonelement
attribute backing field. however, far know, bsonelement
attribute used in query building mongodb c# driver, lose ability use text
property in queries.
the second solution make text
setter private , add method through user of class set text
property, , in bar
method called. however, text
setter used in existing solution, , i'm bit reluctant change 70+ calls across files. plus, code become less readable.
is there cleaner way separate deserialization , user-prompted property change while retaining bsonelement
attribute on property?
why not create seperate property users , db same private variable, this,
public class foo { private string _text; [bsonelement("text"), bsonrequired] public string textdb { { return _text; } set { _text = value; } } [bsonignore] public string text { { return _text; } set { _text = value; bar(_text); } } private void bar(string text) { //only relevant when text set user of class, //not during deserialization } }
Comments
Post a Comment