1

We are using LiteDb to store objects.

An issue we keep having is that people are creating objects containing properties with only public getters. This means this property is not populated when an object is restored from the database.

LiteDb in its default state does not inform us via an exception or otherwise about this issue. Is it possible to change this?

TLDR: We would like to find a way to make LiteDb inform us somehow when an object cannot have a property set when being resurrected back from BSON internally.

Techlead
  • 172
  • 1
  • 10

1 Answers1

4

LiteDB works with Documents that are converted from POCO class using BsonMapper class. BsonMapper map properties from entity class and convert to document fields. Current version (v2) support this rules:

Mapper conventions

BsonMapper.ToDocument() auto converts each property of a class to a document field following these conventions:

  • Classes must be public with a public parameterless constructor
  • Properties must be public
  • Properties can be read-only or read/write
  • The class must have an Id property, Id property or any property with [BsonId] attribute or mapped by fluent api.
  • A property can be decorated with [BsonIgnore] to not be mapped to a document field
  • A property can be decorated with [BsonField] to customize the name of the document field
  • No circular references are allowed
  • Max depth of 20 inner classes
  • Class fields are not converted to document

You can use BsonMapper global instance (BsonMapper.Global) or a custom instance and pass to LiteDatabase in constructor. Keep this instance in a single place to avoid re-creating all mapping each time you use database.


After some users request, next version (v3) will thread BsonMapper as a internal plugin and will support more options like:

  • Read Only properties (custom setting)
  • Internal fields (custom setting)
  • Private properties (custom setting)
  • Custom implementation using IBsonMapper with external JSON.NET support serialization.
mbdavid
  • 1,076
  • 7
  • 8