In a web based application we have an User
object in the session
for each logged in user.
The user is as below
Class User {
//With setter and getters
private String userId,
private String name,
private Account Account,
}
Class Account {
//with setter and getters.
private String accountNumber;
}
When the user logged in a session object is created for him and his userId
,name
and Account
will be set for him.
After that, every programmer can access the session
and read user and his information. But it is possible that one programmer change the Account
by mistake.
For example:
1. TrasnferVO = new TransferVO;
2. TransferVO.setAccount( user.getAccount() );
3. TransferVO.getAccount.setAccountNumber("foo");
4. User user = getUserFromSession();
5. user.getAccountNumber(); // Now I have foo
At line 2 a new copy of user account should be created and set to TransferVO
because the the object reference is passed not its value!
So the line 3 will change the user session account number. And we will lost the corrupted accountNumber
in session.
Is there any guideline to handle it or every individual programmer must take care of it.
Of course this mistake can happen in every code in the project, but I am looking for a way to make the session object properties, which is very important, immutable.