Implement PreSerialize hook on objects

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Environment:
      All
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

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

              Created:
              Updated:
              Resolved: