Ok folks, this time I’ll talk/brag a little bit about the fabulous C# MongoDB driver and how you can write some testable code with it.
If you have come across this post, probably you already know what unit tests and TDD are so you can go directly to the code.
For those of you that have lived under a rock for the last 15 years or so, here are the words of a very wise man.
Take your time, I will be here.
Interesting concept, isn’t it?
Just because I like adding links to my posts, the amazing uncle Bob has a nice list of the “three rules” of TDD.
The basic idea is to have an interface for everything. Yes, simple as that. DI at it’s finest, kids! Always remember to separate your concerns, avoiding huge monolithic classes that do too much. Divide et impera.
We can start with this:
Small and easy. Just basic read and update operations, nothing else. You can find a sample implementation here, as you can see it’s a simple wrapper over the MongoDB driver, nothing else, but having the interface allows you to hide all the implementation details AND to eventually mock everything in your tests.
Enough for now, the new Star Wars movie is waiting for me !
Next time we’ll talk about factories and how to create a simple database context.