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.