HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Is the "current connected user" an information that should be shared with the domain?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thesharedwithuserconnectedthatcurrentshoulddomaininformation

Problem

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

  • When an user connects with his account I save his user id in the


session variable.

  • 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?

Solution

-
I'd create an UserProfilePageDTO to separate that information:

public class UserProfilePageDTO {

    private UserProfileDTO userProfile;

    private Boolean isCurrentUser;
    private Boolean isfollowedByCurrentUser;
    ...
}


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

-
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));
    ...
}

Code Snippets

public class UserProfilePageDTO {

    private UserProfileDTO userProfile;

    private Boolean isCurrentUser;
    private Boolean isfollowedByCurrentUser;
    ...
}
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));
    ...
}

Context

StackExchange Code Review Q#12094, answer score: 5

Revisions (0)

No revisions yet.