Skip to content

Instantly share code, notes, and snippets.

@PathogenDavid
Created October 10, 2019 11:21
Show Gist options
  • Save PathogenDavid/86657500b0e3a2069337ab371c270660 to your computer and use it in GitHub Desktop.
Save PathogenDavid/86657500b0e3a2069337ab371c270660 to your computer and use it in GitHub Desktop.
Small, inefficient modification to LiteDB's BsonMapper to allow recursive/mutually recursive database references.
diff --git a/LiteDB/Mapper/BsonMapper.cs b/LiteDB/Mapper/BsonMapper.cs
index 91a7920e..c6dfee9a 100644
--- a/LiteDB/Mapper/BsonMapper.cs
+++ b/LiteDB/Mapper/BsonMapper.cs
@@ -389,11 +389,11 @@ namespace LiteDB
/// </summary>
private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, string collection)
{
- // get entity
- var entity = mapper.GetEntityMapper(member.DataType);
-
member.Serialize = (obj, m) =>
{
+ // get entity
+ var entity = mapper.GetEntityMapper(member.DataType);
+
// supports null values when "SerializeNullValues = true"
if (obj == null) return BsonValue.Null;
@@ -413,6 +413,9 @@ namespace LiteDB
member.Deserialize = (bson, m) =>
{
+ // get entity
+ var entity = mapper.GetEntityMapper(member.DataType);
+
var idRef = bson.AsDocument["$id"];
return m.Deserialize(entity.ForType,
@@ -427,11 +430,11 @@ namespace LiteDB
/// </summary>
private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collection)
{
- // get entity from list item type
- var entity = mapper.GetEntityMapper(member.UnderlyingType);
-
member.Serialize = (list, m) =>
{
+ // get entity from list item type
+ var entity = mapper.GetEntityMapper(member.UnderlyingType);
+
// supports null values when "SerializeNullValues = true"
if (list == null) return BsonValue.Null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment