Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News A Look at MongoRepository for .NET

A Look at MongoRepository for .NET

Most of the open source projects we share with you on InfoQ increase the capabilities of another library or technology. MongoRepository is different in that it intentionally limits the capabilities of the underlying library. We’ll let Rob Janssen why it does this in his own words.

InfoQ: MongoDB has a surprisingly comprehensive API for .NET. What do you see as the main reason for using MongoRepository instead of just the out-of-the-box drivers?

Rob Janssen: What is surprising to me was that I was asked for this interview at all (I think I have some "more interesting" projects in my 'portfolio' (Github, Codeplex, Nuget...) such as NGeoNames, NIdenticon, TwoFactorAuth, Fop2DD etc.).

Although the API of MongoRepository is well documented, it is far from comprehensive. In fact, it tries to be as 'minimal' as possible. And most (no, let me rephrase that) all hard work is done by Robert Stam, Craig Wilson and other contributors that have implemented the actual, underlying, MongoDB driver. MongoRepository is merely a thin layer, or abstraction, on top of that. It was originally started by tgeek but since I started contributing I kind of have "taken over"; there hasn't been much input nor feedback from him regarding MongoRepository (with which I'm fine).

The reason for the existence of MongoRepository, to me, is simple: it provides a very 'narrow' abstraction on top of the underlying MongoCSharp Driver that, to me, mostly hides all awesome possibilities it provides behind a simple interface that, basically, supports CRUD. This way you don't get too tempted to use all sorts of Mongo-specific functionality and thus high coupling between a project and mongo is minimized so that, should one ever decide to use a different persistence layer, the 'switch' can be made a bit easier. Don't get me wrong though; MongoDB is a great product with awesome features and it's driver is clean, fast and really polished. But sometimes you want to temper temptations to use all this greatness and provide a simpler, more uniform, interface to work with. MongoRepository does just that: it basically provides a Repository pattern interface and that's that. It does offer a few ways to go a bit deeper and provides RepositoryManager classes that hide some Mongo-specific functionality as much as possible but I try very hard to limit this as much as possible to keep coupling to MongoDB a minimum. Sometimes abstractions leak though. They actually most likely will. So does MongoRepository ;-)

InfoQ: Are there any low-level features that you think MongoDB is missing? Anything that, if it existed, would be beneficial to MongoRepository and the projects that use it?

Rob Janssen: I, currently, can't think of any low-level features MongoDB is missing; not 'globally' nor MongoRepository-specific. A repository, to me, needs to support at least get, add, update, delete (CRUD) and this is provided just fine by MongoDB. Other operations can be nice but quickly fall out of the "repository pattern scope". Apart from MongoRepository I have worked with MongoDB quite a bit but I can't say that I miss any features. Earlier versions (pre 2.0 or 2.2) had some issues and quirks but, to my knowledge, most of these things have been fixed, improved or changed for the better. The power of LINQ, and the heavy-lifting done by the mongocsharpdriver, makes MongoRepository a powerful library without too much work done on the MongoRepository side.

InfoQ: Rather than honoring DataContract/DataMember, MongoDB uses its own set of serialization attributes such as BsonElement. Do you agree with this decision or would you have preferred that they stuck with the standard?

Rob Janssen: The BsonElement comes from the underlying MongoCSharpDriver. I still need to refactor this sometime to change it to a MongoRepository-specific attribute but haven't had the time yet. Also I'm not quite sure I have decided *how* I want to implement this. I usually let these kinds of questions sit for a while until I come up with a great ("best") solution but to this date I haven't had that epiphany yet for this issue. However, I am open for discussion and suggestions so anybody with ideas is more than welcome to share!

MongoRepository is offered under the MIT license.

To suggest other open source projects that should be highlighted on InfoQ, contact Jonathan Allen at

Rate this Article