Should controller layer create model objects or they should be created by service layer

controllerservice

I have a small application with classic layers Controller-Service-Dao. Controller actually is REST resource, which deals with JSON data. And the questions are:
1. where is the best place to create business objects from JSON primitives? Controller or Service? Should controller pass primitives from JSON to service methods?
2. If objects should be created in controller layer is it good style to pass in service method primitive value in case it is some kind of search method by id?

UPDATE 1.
I'm talking about java:

serviceSearchMethod(int value1, String value2);

vs

SomeObject someObject = new SomeObject(int value1, String value2);
serviceSearchMethod(someObject);

Of course, it can be that someObject contains 10 fields, but controller has only 2 values, so is it good in this case create business object (BO) or it is suitable only in case I can create BO which is not just DTO to service layer, but something valuable from business point of view?

Best Answer

I prefer to convert the JSON to business objects as soon as possible, and generally this is done in the REST controller class. There are reasons for this:

  • JSON is a data transport format. In a large system there may well be other data transport formats (eg XML or CSV). So if possible your internal representation should be data format independent.
  • Business objects usually have behavioral methods. If the data is still in JSON format, then best of luck with writing behavioral methods :)
  • In most systems, objects are created in code as well as arriving on the wire. It would be weird to have to create a JSON object rather than a Java POJO.

But a caveat - my experience has been with Java REST services. If you are running a server-side Javascript framework, then other considerations may apply.