Created
March 15, 2014 13:41
-
-
Save jlesech/9567469 to your computer and use it in GitHub Desktop.
Mono-Mongo/Serialization with ClassMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MongoDB.Bson.Serialization; | |
using MongoDB.Driver; | |
namespace Race | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
BsonClassMap.RegisterClassMap<Participant> | |
( | |
cm => | |
{ | |
cm.AutoMap(); | |
cm.GetMemberMap(c => c.Age).SetIgnoreIfNull(true); | |
} | |
); | |
// Get a Reference to the Client Object. | |
string connectionString = "mongodb://localhost"; | |
MongoClient client = new MongoClient(connectionString); | |
// Get a Reference to a Server Object. | |
MongoServer server = client.GetServer(); | |
// Get a Reference to a Database Object. | |
MongoDatabase database = server.GetDatabase("Race"); | |
// Get a Reference to a Collection Object. | |
MongoCollection collection = database.GetCollection<Participant>("Participants"); | |
// Insert a Document. | |
Participant participant1 = new Participant("Jean-Claude", "Duss", 62); | |
collection.Insert(participant1); | |
Participant participant2 = new Participant("Thierry", "La France"); | |
collection.Insert(participant2); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Race | |
{ | |
public class Participant | |
{ | |
#region Fields | |
private string firstName = string.Empty; | |
private string lastName = string.Empty; | |
private Nullable<byte> age = null; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Race.Participant"/> class. | |
/// </summary> | |
/// <param name='firstName'> | |
/// First name. | |
/// </param> | |
/// <param name='lastName'> | |
/// Last name. | |
/// </param> | |
public Participant(string firstName, string lastName) | |
{ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Race.Participant"/> class. | |
/// </summary> | |
/// <param name='firstName'> | |
/// First name. | |
/// </param> | |
/// <param name='lastName'> | |
/// Last name. | |
/// </param> | |
/// <param name='age'> | |
/// Age. | |
/// </param> | |
public Participant(string firstName, string lastName, Byte age) | |
{ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.age = age; | |
} | |
#endregion | |
#region Properties | |
/// <summary> | |
/// Gets or sets the first name. | |
/// </summary> | |
/// <value> | |
/// The first name. | |
/// </value> | |
public string FirstName | |
{ | |
get { return this.firstName; } | |
set { this.firstName = value; } | |
} | |
/// <summary> | |
/// Gets or sets the last name. | |
/// </summary> | |
/// <value> | |
/// The last name. | |
/// </value> | |
public string LastName | |
{ | |
get { return this.lastName; } | |
set { this.lastName = value; } | |
} | |
/// <summary> | |
/// Gets or sets the age. | |
/// </summary> | |
/// <value> | |
/// The age. | |
/// </value> | |
public Nullable<byte> Age | |
{ | |
get { return this.age; } | |
set { this.age = value; } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment