Where do I initialize a database?

Repository Pattern with MongoDB: Where to initialize the Database

  • I just started to play around with MongoDB (C#) and tried to port a repository over from entity framework. I'm using the official C# driver 1.0. Now I did something like this: internal class MongoContext { public MongoContext(string constring) { MongoServer server = MongoServer.Create(constring); this.myDB = server.GetDatabase("MyDB"); BsonClassMap.RegisterClassMap<VoyageNumber>(cm => { cm.MapField<string>(p => p.Id); }); BsonClassMap.RegisterClassMap<Schedule>(cm => { cm.MapField<DateTime>(p => p.EndDate); cm.MapField<DateTime>(p => p.StartDate); }); BsonClassMap.RegisterClassMap<Voyage>(cm => { cm.MapIdField<VoyageNumber>(p => p.VoyageNumber); cm.MapField<Schedule>(p => p.Schedule); }); } private MongoDatabase myDB; public MongoDatabase MyDB { get { return this.myDB; } } } I'd then go on and implement the Repository like this: public class MongoVoyageRepository : IVoyageRepository { private readonly MongoContext context; public MongoVoyageRepository(string constring) { this.context = new MongoContext(constring); } public void Store(Domain.Model.Voyages.Voyage voyage) { MongoCollection<Voyage> mongoVoyages = context.MyDB.GetCollection<Voyage>("Voyages"); //store logic... } } Now I'd like to know if it is a good decision to instantiate a "context" like this in terms of performance. Does it make sense to put the BsonClass Maps in there? Thank you for your input.

  • Answer:

    I guess it's does not make sense to register classes mapping each time when you create your repository class. Since mongodb c# driver manage connetions to the mongodb internally it seems to me that better create MongoServer and register classes mapping only once, during application start and than use it. I am using singletone in order to create MongoServer only once public class MongoRead : MongoBase { public MongoRead(MongoServer server) : base(server) { } public override MongoDatabase Database { get { return Server.GetDatabase("myDb"); } } public MongoCollection Logs { get { return Database.GetCollection("logs"); } } private static MongoRead _instance = null; public static MongoRead Instance { get { if (_instance == null) { _instance = RegisterMongoDb(); } return _instance; } } private static MongoRead RegisterMongoDb() { var readServer = MongoServer.Create(connectionString); var read = new MongoRead(readServer); var myConventions = new ConventionProfile(); myConventions.SetIdMemberConvention(new NoDefaultPropertyIdConvention()); BsonClassMap.RegisterConventions(myConventions, t => true); return read; } } So you also can use above class in your Repository: public class MongoVoyageRepository : IVoyageRepository { private readonly MongoRead context { get { return MongoRead.Instance; } }; public MongoVoyageRepository() { } public void Store(Domain.Model.Voyages.Voyage voyage) { MongoCollection<Voyage> mongoVoyages = context.Database.GetCollection<Voyage>("Voyages"); //store logic... } }

Malkier at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.