微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MongoDB .NET 中自定义 IDictionary 的序列化问题

如何解决MongoDB .NET 中自定义 IDictionary 的序列化问题

我有一个类,它实现了 IDictionary 并限制了键值的类型和范围。 (基本上,一个围绕本地字典的包装器,具有对键的相关验证和整数的固定键类型,我称之为 RestrictedKeyDictionary)。

我知道我无法序列化为“文档”,因为我的键不是字符串。但是,如果我选择 ArrayOfDocuments 或 ArrayOfArrays,我仍然会遇到与选择文档模式相同的运行时错误使用 DictionaryRepresentation 时。文档 键值必须序列化为字符串

因此,驱动程序中的错误或更可能是我设置序列化的方式。

在下面的示例中,我的成员是 RestrictedKeyDictionary(Of String),其中 String 是存储的值的类型。在底层代码中键被固定为整数。

根据文档,我想出了这个序列化程序(VB.Net):

cm.GetMemberMap(Function(p) p.MyProperty).SetSerializer(New DictionaryInterfaceImplementerSerializer(Of RestrictedKeyDictionary(Of String),Integer,String) _
(Options.DictionaryRepresentation.ArrayOfDocuments))

请注意我在此处选择的 ArrayOfDocuments,尽管如此,还有关于 Document 模式的错误消息。如果我选择 ArrayOfArrays,结果相同。

有人有什么想法吗?还有谁知道关于 MongoDB .NET 序列化的非常好的彻底教程? API 文档虽然内容丰富,但没有提供类的上下文,参考教程也有不足之处。

作为参考,我在这里对 RestrictedKeyDictionary 的整个实现:

Public Class RestrictedKeyDictionary(Of TValue)
    Implements IDictionary(Of Integer,TValue)
    Private _dct As New Dictionary(Of Integer,TValue)
    Private _minkey As Integer
    Private _maxkey As Integer

    Default Public Property Item(key As Integer) As TValue Implements IDictionary(Of Integer,TValue).Item
        Get
            Return _dct.Item(key)
        End Get
        Set(value As TValue)
            Add(key,value)
        End Set
    End Property

    Public ReadOnly Property Keys As ICollection(Of Integer) Implements IDictionary(Of Integer,TValue).Keys
        Get
            Return _dct.Keys
        End Get
    End Property

    Public ReadOnly Property Values As ICollection(Of TValue) Implements IDictionary(Of Integer,TValue).Values
        Get
            Return _dct.Values
        End Get
    End Property

    Public ReadOnly Property Count As Integer Implements ICollection(Of keyvaluePair(Of Integer,TValue)).Count
        Get
            Return _dct.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of keyvaluePair(Of Integer,TValue)).IsReadOnly
        Get
            Return CType(_dct,ICollection(Of keyvaluePair(Of Integer,TValue))).IsReadOnly
        End Get
    End Property

    Public Sub Add(key As Integer,value As TValue) Implements IDictionary(Of Integer,TValue).Add
        If key < _minkey Or key > _maxkey Then
            Throw New ArgumentOutOfRangeException("key",$"key must be between {_minkey} and {_maxkey}")
        Else
            _dct.Add(key,value)
        End If
    End Sub

    Public Sub Add(item As keyvaluePair(Of Integer,TValue)) Implements ICollection(Of keyvaluePair(Of Integer,TValue)).Add
        Add(item.Key,item.Value)
    End Sub

    Public Sub Clear() Implements ICollection(Of keyvaluePair(Of Integer,TValue)).Clear
        CType(_dct,TValue))).Clear()
    End Sub

    Public Sub copyTo(array() As keyvaluePair(Of Integer,TValue),arrayIndex As Integer) Implements ICollection(Of keyvaluePair(Of Integer,TValue)).copyTo
        CType(_dct,TValue))).copyTo(array,arrayIndex)
    End Sub

    Public Function ContainsKey(key As Integer) As Boolean Implements IDictionary(Of Integer,TValue).ContainsKey
        Return _dct.ContainsKey(key)
    End Function

    Public Function Remove(key As Integer) As Boolean Implements IDictionary(Of Integer,TValue).Remove
        Return _dct.Remove(key)
    End Function

    Public Function Remove(item As keyvaluePair(Of Integer,TValue)) As Boolean Implements ICollection(Of keyvaluePair(Of Integer,TValue)).Remove
        Return CType(_dct,TValue))).Remove(item)
    End Function

    Public Function TryGetValue(key As Integer,ByRef value As TValue) As Boolean Implements IDictionary(Of Integer,TValue).TryGetValue
        Return _dct.TryGetValue(key,value)
    End Function

    Public Function Contains(item As keyvaluePair(Of Integer,TValue)).Contains
        Return CType(_dct,TValue))).Contains(item)
    End Function

    Public Function GetEnumerator() As IEnumerator(Of keyvaluePair(Of Integer,TValue)) Implements IEnumerable(Of keyvaluePair(Of Integer,TValue)).GetEnumerator
        Return CType(_dct,IEnumerable(Of keyvaluePair(Of Integer,TValue))).GetEnumerator
    End Function

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return CType(_dct,IEnumerable).GetEnumerator
    End Function

    Public Sub New(MinKey As Integer,MaxKey As Integer)
        _minkey = MinKey
        _maxkey = MaxKey
    End Sub
End Class

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。