4
\$\begingroup\$

I'm working right now in a readers social network. The main points are this:

  1. When an user connects with his account I save his user id in the session variable.
  2. My MVC is working with DTO's all the way, filling the necessary variables for the presentation in the model.

So, everything was ok, until today that I had to do a function that returns a DTO that has all the information of an user profile page. User profile pages can be accessed by anyone who has an account (Like Facebook user pages).

This profile page, shows a button "Follow" if the profile is not the profile of the current user (to avoid you following yourself). If we are following that user, it will display an unfollow button.

So I was thinking that the UserProfileDTO must have two boolean:

isTheCurrentUser and isFollowedByTheCurrentUser.

public class UserProfileDTO {
    private Long id;
    private String username;

    private Boolean isCurrentUser;
    private Boolean isfollowedByCurrentUser;
...

And the Domain Controller

public User getUserProfile(String username, Long currentUserId) throws UserNameDoesntExist {
         UserProfileDTO user = dataController.getUserByUsername(username);
         if(user != null) {     
               user.setIsCurrentUser(currentUserId == user.getId());
           user.setIsFollowedByCurrentUser(dataController.isFollowed(user.getId(), currentUserId));
          ...

But I thought, "This DTO will be filled with information at the domain model. So the domain must have information that the presentation had first. That information is the current connected user, thus that will create a dependency between the two layers.

Am I correct? Should the domain model be ignorant about who the current connected user is? Or is what I am doing okay?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$
  1. I'd create an UserProfilePageDTO to separate that information:

    public class UserProfilePageDTO {
    
        private UserProfileDTO userProfile;
    
        private Boolean isCurrentUser;
        private Boolean isfollowedByCurrentUser;
        ...
    }
    
  2. UserNameDoesntExist - it seem unusual a little bit because of the nt. I'd call it UsernameDoesNotExists or UserNotFoundException (same pattern as ClassNotFoundException and FileNotFoundException from JDK).

  3. I'd consider using a guard clause:

    public User getUserProfile(String username, Long currentUserId) 
            throws UserNotFoundException {
        UserProfileDTO user = dataController.getUserByUsername(username);
        if (user == null) {     
            throw new UserNotFoundException(...);
        }
        user.setIsCurrentUser(currentUserId == user.getId());
        user.setIsFollowedByCurrentUser(dataController.isFollowed(user.getId(), currentUserId));
        ...
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.