Reputation: 2030
I have a User
model. A user can be reviewed. Thus I also have a Review
model that represents a review of the user. With the review a rating of the user is saved.
Let's say I am to create a method that fetches the rating of the user based on all reviews of him, for example the getRating()
method. Would I create this method on the User
model? Or would you create for example a review-managing service class and include the method in that class?
Adding the getRating()
method to the User
model feels like a logical thing to do, but on the other hand things might get out of hand at one point, leaving me with a huge User
model class.
(This is a simplified example, in reality the reviews can come from multiple sources).
(PS I have read some posts and articles about this, but I cannot find a satisfactory answer to my question).
Upvotes: 0
Views: 270
Reputation: 522451
You shouldn't have "a User
model" and "a Review
model". The Model in MVC is your entire core app, it encompasses all data structures, services, database adapters and auxiliary stuff that makes your app work. What you should have is something like:
User
business object and a Review
business object, purely defining the data structuresUserRepository
or UserDAO
or UserORM
or whatever other paradigm you use to interact with your database which translates data in the database to/from User
objects; same for Review
UserService
which does things for user related tasks, like storing and retrieving them via the database interface, same for Review
, or perhaps some differently named service being responsible for both togetherIt doesn't all have to map 1:1; a business object instance may map to multiple database tables, which the corresponding *Repository
/*DAO
/whatever takes care of, and multiple of those may be combined into one service making something useful happen.
The action to retrieve some specific data then belongs into a service.
Upvotes: 1
Reputation: 133
Why not add the getRating()
to the Review
Model. The method will accept a User
Object or a User Id depending on the logic in the method.
The User Model has no business knowing about reviews.
My thoughts though.
Upvotes: 0