1

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
cafesanu
  • 435
  • 1
  • 4
  • 12
  • It could work either way, but I would try to put that check inside the controller rather than the view. Think in terms of trying to make a test for the controller with a fake view such that any common logic that might exist between the fake view and the real view are refactored into the controller. – Vaughn Cato Mar 03 '12 at 22:09

1 Answers1

0

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...

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61