At which MVC level should I implement context manager


#1

Hi everyone.

I am planing to implement the use of the with statement to properly close the connection to my devices, should an error occur, and I was wondering at which level should I implement it. My quick guess is that it should be done at the controller level, but I am not sure if that will actually do the job properly when you are calling the controller from the higher layers.
Thanks!


#2

Hello Martín!
Yes, doing it at the controller level is a good idea, because is where the real communication with your device happens, and is what you want to ensure follows the same pattern of connecting/disconnecting. You will have to test what works best for you.

You could also have a two layer of context managers, one for the controller and one for the model. Perhaps there is something you would like to ensure before disconnecting (closing shutters, etc.) that belongs more to the model than to the controller…