简体   繁体   中英

Should c# mongodb documents inherit from BsonDocument

Should mongodb document classes written in c# inherit from BsonDocument ?

For example:

/// <summary>
/// Company document
/// </summary>
public class CompanyDocument : BsonDocument
{
    /// <summary>
    /// Collection name
    /// </summary>
    public const string COLLECTION_NAME = "company";

    /// <summary>
    /// Unique company name
    /// </summary>
    [BsonElement("name")]
    public string Name
    {
        get;
        set;
    }

    /// <summary>
    /// List of referenced users
    /// </summary>
    [BsonElement("users")]
    public IList<MongoDBRef> Users
    {
        get;
        set;
    }
}

Because when i want to query data, it seems that I need to inherit from BsonDocument . Or should it be more like a POCO object or inherit from some thing else?

Thank you very much!

I know this is an old question, but in case someone stumbles here looking for an answer like I did. The answer is NO.

While it is possible, it has been noted to cause many problems. The one I saw specifically was with serialization.

I ran across these two bug reports:

A few excerpts from them that explain it better than I can:

If you really want to subclass BsonDocument what you will need to do is wire things up so that your MyBsonDocument values get serialized correctly. -- Robert Stam


BsonDocument can represent any structure that exists. So, the only reason to inherit is (a) to provide a different behavior (RawBsonDocument, LazyBsonDocument), or (b) to provide strongly-typed access to certain fields. -- Craig Wilson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM