0

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!

5
  • i guess both are the same, it only depends on the parameter you want to use. the Member object or a String parameter
    – Ker p pag
    Nov 19, 2014 at 6:58
  • @Kerppag This application will become distributed. Maybe method B is better?
    – Neo
    Nov 19, 2014 at 7:02
  • in terms of code readability i think method B is better. btw are you using Spring @Neo ?
    – Ker p pag
    Nov 19, 2014 at 7:03
  • I would suggst option X. none of the above. I'm a huge fan of protecting your domain and your domain should only be accessed from the services. Create an object that holds the parameters and use that object to construct a member inside the service. Depending on what you prefer it could be a MemberDto, CreateMemberRequest or CreateMemberCommand. This also solves the problem of for instance where to store/leave the verifyPassword field (as that in general isn't part of your Member). Added bonus is that your architecture is now more about intentions of your code.
    – M. Deinum
    Nov 19, 2014 at 7:07
  • @M.Deinum Thanks your comment. I agree with your opinion "domain should only be accessed from the services"
    – Neo
    Nov 19, 2014 at 7:13

1 Answer 1

2

there is a third approach

With in your controller you will get your Member object as input and you can use this to call the method in your service.

@RequestMapping(value = "/saveMember", method = RequestMethod.POST)
@ResponseBody
public String saveAddress(@RequestBody Member member) {
    accountService.saveMember(member);
    ....
}

the following is the javascript code that makes the call:

saveMember = function() {
    var saveURL = '/saveMember';

    var memberData = {};
    memberData.username = 'some user name';
    memberData.password = 'some password';
    .... (your other fields)

    $.ajax({
        url : saveURL,
        method : 'POST',
        dataType : 'json',
        contentType : 'application/json',
        data : JSON.stringify(memberData),
        success : function(result) {
            ...
        }, error: function(errorObject) {
            console.log(errorObject);
        }
    });
};
3
  • public String saveAddress(@RequestBody Member member){}? Using @RequestBody annotation will lead to org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported. Should I use@ModalAttribute instead?
    – Neo
    Nov 20, 2014 at 7:22
  • check this and this Nov 20, 2014 at 7:34
  • and one more thing is your Member class is serializable?? If not then implement your Member class with Serializable interface and check once. Nov 20, 2014 at 7:37

Your Answer

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

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