Anonimous
Anonimous

Reputation: 41

BSON serialization in C# driver for MongoDB

I just started playing with MongoDB and official c# driver for it. And I have a small question regarding objects serialization. For example we have a classes:

public class User
{
    public string Name;

    public List<Comment> Comments = new List<Comment>(){ new Comment() };
    public List<Card> Cards = new List<Card>() { new Card() };
}

public class Comment
{
    public string Id;
    public string Text;
}

public class Card
{
    public string Id;
    public string Text;
}

I want to get serialized Cards collection within User, but Comments collection like DBRef. Is it possible to achieve it with latest standard c# driver? It will be really cool to use some attribute like:

public class User
{
    public string Name;

    [UseDBRef]
    public List<Comment> Comments = new List<Comment>(){ new Comment() };

    public List<Card> Cards = new List<Card>() { new Card() };
}

Upvotes: 3

Views: 1955

Answers (2)

Viraj Siriwardana
Viraj Siriwardana

Reputation: 41

Have a look at this project in GitHub.

https://github.com/virajs/MongoDB-Mapping-Attributes.git

This project mainly provide you with two mapping attributes. OneToMany and ManyToOne. Checkout the code and play around with the test project.

Upvotes: 1

Robert Stam
Robert Stam

Reputation: 12187

You could declare your Comments property as List<MongoDBRef> and handle the relationship yourself but there is no automatic support for that.

Upvotes: 0

Related Questions