[CSHARP-855] Interface to allow control of object serialization Created: 29/Oct/13  Updated: 11/Mar/19  Resolved: 06/Apr/15

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.8.3
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Curt Mayers Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All



 Description   

A suggestion.

Provide a pre-defined C# interface to control whether entire object instances should be serialized (e.g. IControlledSerialization).

I have many objects that are part of documents that get saved to Mongo. Very often, I will not want to serialize that object at all, depending upon conditions within that object instance. Rather than having to write separate ShouldSerialize[PropertyName] for each field in which that object appears, it would be simpler if the object could determine for itself whether it should be persisted.

I have written a custom convention to implement this scheme, but it seems like it may be of more general use to the user community, and would be pretty simple to impement:

public interface IControlledSerialization
{
bool ShouldSerialize();
}

Classes that have consistent criteria for serialization can then implement this interface, and use whatever criteria are appropriate for the action.

If the interface (and the corresponding convention) were built into the driver, I think it would be useful to many.



 Comments   
Comment by Craig Wilson [ 30/Oct/13 ]

Certainly makes sense. As you have written a convention to do this already, I guess all you are stating is that you would like this to be built-in to the driver, The downside to this, of course, that now your objects have a dependency on MongoDB where-as they may not have previously. The way you have done it, you can define whatever interface you want and hook it up.

We'll leave this ticket open, but not schedule it for implementation until we have talked it through. Thanks for your input.

Comment by Curt Mayers [ 30/Oct/13 ]

Craig -

I think you misunderstood what I was suggesting.

In my business object model, there are many (many, many!) constituent objects which carry optional data chunks (each of which is represented by a specialized class). In such cases, it often worked out more cleanly to have those sub-objects determine for themselves whether they should be serialized--obviating the necessity of defining individual ShouldSerialiseXXX methods for individual properties on each the master objects in which they occur.

I've worked this out by defining a simple, optional, interface I can affiliate with such subobject classes. The interface can let the object decide whether it should be serialized at all, rather than the owning object deciding on a field-by-field basis.

As a concrete example, in the business classes I'm writing, master objects have a complex "Provenance" object when the data came from an outside system. Almost every master object type has a provenence field (or collection of them), so they probably occur within twenty different master classes that get serialized to Mongo.

It is easier and cleaner, in this case, for the Provenance object to decide whether it should be serialized than implementing twenty or more ShouldSerializeXXX methods in each of the master classes. It turned out to be pretty simple to implement, and I thought that providing a native Interface and convention for doing this would be something helpful to the community.

Does this make sense to you?

Thanks again for your quick response.

Curt

Comment by Craig Wilson [ 29/Oct/13 ]

The documentation here (http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/#ignoring-a-member-based-on-a-shouldserializexyz-method) indicates how to do this. You can provide a ShouldSerializeXXX method where XXX is the name of your member. Inside it, you can do anything you want and the results of this method will instruct the serialization infrastructure on whether or not to serialize that particular member.

Craig

Generated at Wed Feb 07 21:38:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.