I am not sure if I am supposed to user domain objects directly in the user interface. For example I wish to design the user interface for the domain entity user User wich has a user id, name, password and a list of roles. The entity is designed in such a way that it can never be in an invalid state (for example an invalid password or empty userId)
public class User {
public User(String userId, String name, String password) {
//Initialization and validation
}
public String getUserId {
/*implementation*/
}
public void changePassword(String oldPassword, String newPassword) {
/*Set new password if it complies with the rules
and if the the old one is correct*/
}
public void setName(String Name) {
/*implementation*/
}
public String getName() {
/*implementation*/
}
public List<Role> getRoles() {
/*implementation*/
}
public void addRole(Role role) {
/*implementation*/
}
}
What is the most appropriate way to design the user interface?
1) Stick to the domain model: Design 3 windows. Window "New user" creates a new user with the given userId, name and password. Another window "Change password" to change the password and another one "Modify user" that lets you modify the name and roles of an existing user
2) It could be desirable to use only one window to create the user with the given userId, name, password and list of roles. I should be able to add roles even when I haven't typed the userId yet.
Option 1 is the easiest to implement because I can use the domain objects in the user interface directly. But the user interfaces could result painful to use. Option 2 is desirable but the domain objets are not usable directly. I do not see how can I add roles if the user is not created yet. I cannot create the user because I might not have the correct information at that moment, for example a temporary empty textbox that represents the userId. How can I achieve this?
The only clean solution I can think of is creating classes to use in the user interface that will mimic the information in the real domain objects. So I will be able to create an empty User to add Roles, then set the userId after that. The user interface can use that information to create the real domain objet.
Is there an easy way around this? How is this usually managed?