Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-710

Implement PreSerialize hook on objects

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None
    • All

    Description

      Bson serialization allow objects to implement ISupportInitialize
      and it will call appropriate methods. But I had not found any opposite method.
      I think, it would be great to have some custom interface, e.g.:
      interface IBsonSerializationHook
      {
      void BeforeSerialize();
      void AfterDeserialize();
      }
      So it will be easy to do some work before serialization and after deserialization.
      I have a use case for this:
      There is a document per user {_id:123, ReadArticleIds:[1,2,3,4,5.... 10000]}
      ReadArticleIds may contain thousands of Ids. If i just save/load it, it takes forever (more then a second for one record) to load.
      But, I know, that I need ReadArticleIds only in memory, I will not be doing any queries against it. So I serialize it with BinaryFormatter and save it in BsonBinaryData. Now it will load/save 100 times faster at least. Even with overhead of binaryformatter.

      I tried to implement IBsonSerializable and inside do my stuff and just call BsonSerializer.Seriaze(). But that obviuously caused stack overflow

      Also, I just don't like doing such hacky customization. Code would be much easier to understand with appropriate interface/method names.

      Attachments

        Activity

          People

            Unassigned Unassigned
            maxbl4 Vladimir Perevalov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: