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
Related Q & A:
- Where is my Code First database?Best solution by Stack Overflow
- Does Restore "From database" restore from the most recent backup of the database or the actual live database?Best solution by Database Administrators
- where is the index physically located in MySQL database?Best solution by Database Administrators
- Where can I keep a database?Best solution by Stack Overflow
- Does Canada have a criminal database where you can look up offenders like you can here in the US?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.