-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: 2.18.0
-
Component/s: Serialization
-
None
A common paradigm is to create a private backing field for a public property on a C# class (POCO). Often times there is logic in the property setter to validate input, modify a related field or record an event log/history of the change. This logic should not be performed when deserializing the object from the database. Currently, the only alternative is to provide a creator/constructor method. This can be painful for classes with many properties, especially if only one ( or a few) property(ies) have backing fields and setter logic that should be skipped on re-hydration. It can also lead to bugs when new properties are added but developers forget to add them to the constructor.
By allowing callers to set a custom setter(and getter) actions, developers can still AutoMap the class, but customize individual properties that require special handling. Something like:
public BsonMemberMap SetPropertySetter(Action<object, object> setter)
Ideally, there would also be a overload like this:
public BsonMemberMap SetPropertySetter(string memberName)
This overload will allow developers to simply specify an alternate member, like a private backing field, to use when setting the value during deserialization.
There are probably fewer use cases for customizing the Getter, but it could still be valuable and would maintain symmetry:
public BsonMemberMap SetPropertyGetter(Func<object, object> getter)
public BsonMemberMap SetPropertyGetter(string memberName)
- is duplicated by
-
CSHARP-3531 Init properties sometimes are not serialized (not consistent with readonly properties)
- Closed