I have a @Service
bean which function is to save a new Member
entity to database:
@Service
public class AccountService {
// method A
public void saveMember(Member m) {
entityManager.persit(m);
}
// method B
public void saveMember(String username, String pwd, int age /* ... lots of parameters ... */) {
Member m = new Member();
m.setUsername(username);
m.setPassword(pwd);
// ... ...
entityManager.persit(m);
}
}
I will call saveMember()
in @Controller
bean directly. Which method is better?
If I use method A, then I have to construct Member
entity in @Controller
bean, showing as bellow:
@Controller
public class Controller {
public String profile(@RequestParam String username,
@RequestParam String password
// ... ...
) {
Member m = new Member();
m.setXXX();
// ... ...
// lots of setters method invocation
accountService.saveMember(m);
}
}
If using Method B:
@Controller
public class Controller {
public String profile(@RequestParam String username,
@RequestParam String password
// ... ...
) {
accountService.saveMember(username, password /* lots of parameters */);
}
}
Which is better? Thanks!