I plan to write webapp in python using Flask and MongoDB (and probably Ming as ODM). The problem is that I would like my model and controller to be really well separated, one of the reasons for this was to be able to run simple unittests on separate components.
Now here is my problem, at some point in the request life cycle, I need to connect to MongoDB. Each request will have a separate connection. Flask offers a local thread object that can contain any variables that are global for the request, this seems to be a good place to establish a mongo connection. However, this creates a tough relationship between the data layer and the flask, which will make testing or managing them separately quite difficult.
So my question is whether there is an elegant solution for this. I myself came up with a couple of options, but they are very far from elegance.
At first, I could just provide the data module with a function that would tell her where to get the connection object from. Or, in a similar way, give it a function that it can use to retrieve a new connection.
The second option is to create a class that the module can use to connect to MongoDB, and then create 2 versions of this class, which uses the global Flask object, and the other simply connects to MongoDB.
Both of them do not seem really reliable or elegant for me, is there any way to do this better?
source share