I’m implementing a Model View Control program.
I have a class User that has a list of photo albums, so I have a method addAlbum(String name).
My question is, since the controller is supposed to verify that all data is valid, should the controller verify that user doesn’t have an album with that name. in other word, should the precondition of addAlbum be that the album doesn’t exist, or is it OK to traverse the list of albums (inside class user), verifying that album doesn’t exist?
Your controller verifies that all data is indeed valid, but that’s the data that the user inserts in the View (e.g. validate that the provided name of the album is non-empty).
The Controller should then invoke the Model to add the album to the User.
The Model – maybe by using a service layer – now verifies if the user doesn’t have that album, traversing the list of albums, validating business rules etc.
Normally in MVC, the User will just be data on which the Model operates (i.e. User class does not do business logic or validations, the Model does those).
At least that’s how I would do it with MVC in a situation like this…