My suggestion would be to encapsulate this code into the model.