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!

Neo
  • 2,196
  • 5
  • 32
  • 58
  • 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 '14 at 06:58
  • @Kerppag This application will become distributed. Maybe method B is better? – Neo Nov 19 '14 at 07:02
  • in terms of code readability i think method B is better. btw are you using Spring @Neo ? – Ker p pag Nov 19 '14 at 07: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 '14 at 07:07
  • @M.Deinum Thanks your comment. I agree with your opinion "`domain should only be accessed from the services`" – Neo Nov 19 '14 at 07:13

1 Answers1

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);
        }
    });
};
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
  • `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 '14 at 07:22
  • check [this](http://stackoverflow.com/questions/18301743/how-to-use-requestbody-with-a-jsonp-request) and [this](http://stackoverflow.com/questions/19413023/spring-rest-view-resolution-unsupported-content-type) – Prasad Khode Nov 20 '14 at 07:34
  • and one more thing **is your Member class is serializable??** If not then **implement your Member class with Serializable interface** and check once. – Prasad Khode Nov 20 '14 at 07:37