c# driver and best practices/patterns for mongo access

2,164 views
Skip to first unread message

mrdiesel

unread,
Jan 3, 2012, 3:36:56 AM1/3/12
to mongodb-user
I want to use mongodb for a project.

OK yes, i able to connect to it and do things.

However, I want to be able to wrap the mongodb driver around a wrapper
so if i want to change the database, i want to be easily change it.

Repository pattern looks like something I can use with Mongodb, but I
m kinda over thinking where to start the connection? should i use a
singleton ?

Mongodb docs says that it is thread safe, so if i create a single
MongoServer and single MongoDatabase classes via Singleton and per
collection, I can have a method that return a collections.

Is this a bad design?

What would be a better design?

I want to use singleton to create connection to database cause I dont
want to keep creating connection per thread, if there is already a
persistent connection.

any experience with this?

Nat

unread,
Jan 3, 2012, 7:38:12 AM1/3/12
to mongod...@googlegroups.com

Robert Stam

unread,
Jan 3, 2012, 10:11:06 AM1/3/12
to mongod...@googlegroups.com
It's OK to create a singleton in your code to hold a single reference to an instance of MongoServer (or even MongoDatabase or MongoCollection).

Note that as the documentation states whenever you call MongoServer.Create with the same connection string you get the SAME instance of MongoServer back, so it is not strictly necessary for you to create a singleton reference in your code.

Having a singleton reference in your code would be minutely faster, only because it eliminates a couple of dictionary lookups that MongoServer.Create does to check whether an instance of MongoServer already exists for that connection string.

On Tue, Jan 3, 2012 at 7:38 AM, Nat <nat....@gmail.com> wrote:
Already asked at  http://stackoverflow.com/questions/8709665/patterns-for-mongodb-c-sharp-client

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/CVbNz8zAAb0J.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

mrdiesel

unread,
Jan 6, 2012, 12:47:16 PM1/6/12
to mongodb-user
I asked there as well, same question, i dont think u get all the
answers at stackoverflow. so wanted to ask here as well. if u dont
mind.

On Jan 3, 4:38 am, Nat <nat.lu...@gmail.com> wrote:
> Already asked at  http://stackoverflow.com/questions/8709665/patterns-for-mongodb-c-sha...
Message has been deleted

Robert Stam

unread,
Jan 6, 2012, 12:50:24 PM1/6/12
to mongod...@googlegroups.com
I don't mind. It just gets confusing when there are two parallel threads going on... :)

Did you get all your questions answered?

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
Reply all
Reply to author
Forward
0 new messages