Jan Hoppmann wrote:I had a discussion with a colleague about the placement of a method. The setup is this: I have project based on the MVC pattern. I have a controller, I have a User-class and I have a class that manages my input and output. Now my user tries to login. I thought my controller should ask the database for a User object and use this. My colleague said that it would be better to create a new User object in the controller, and leave reading to it (so that the user knows about the input and output class). Same goes for saving the User (in case of changes).
Both versions work, of course - but what would be better?
Interesting question.
On balance, I would say I agree with your colleague: that the login process should be part of the controller, and that it should hand you back a fully-functioning User object - and certainly one that knows how to save itself.
However, that doesn't preclude you from having an object that handles I/O; it just means that functions that have anything to do with a User are dealt with
by that User - I presume there's nothing to stop your User object from calling your I/O manager if it needs to.
Winston