Reputation: 2953
I am checking out my project code and found below method in controller. On the net I found that controller
is for receive request and provide response. Service
Layer is for Business Logic and Dao
layer is for data CRUD related operation.
In below method I can see business logic. Now I am not getting which code should be moved to service layer or below is fine.
I am reviewing code so I need to provide comments but I am confused.
@RequestMapping(value = "/admin/app", method = RequestMethod.POST)
public ModelAndView saveApp(
@ModelAttribute("application") @Validated Application application,
BindingResult result) {
ModelAndView model = new ModelAndView();
ApplicationFormValidator formValidation = new ApplicationFormValidator();
boolean messageFlag = false;
String operationalStatus = null;
formValidation.validate(application, result);
if (result.hasErrors()) {
model.addObject(APPLICATION, application);
model.setViewName(ADD_APP);
} else {
if(checkActive(application)){
status = FormBeanValidator.encodeStatus(application.getStatus());
application.setStatus(status);
// calling service layer and convert model into entity
messageFlag = applicationService.addApp(application);
if (messageFlag) {
Application applicationForm = new Application();
applicationForm.setSuccessMessage(PropertyHandler.getPropertyInstance().getPropertyValue(Constants.SUCCESS_MESSAGE));
model.addObject(APPLICATION, applicationForm);
model.setViewName(ADD_APP);
} else {
application.setErrorMessage(PropertyHandler.getPropertyInstance().getPropertyValue(Constants.ERROR_MESSAGE));
model.addObject(APPLICATION, application);
model.setViewName(ADD_APP);
}
}
else{
application.setErrorMessage(PropertyHandler.getPropertyInstance().getPropertyValue(Constants.OTHER));
model.addObject(APPLICATION, application);
model.setViewName(ADD_APP);
}
}
return model;
}
Upvotes: 0
Views: 1469
Reputation: 243
The code looks fine,but I would suggest some modifications:
1) Your checkActive(application)
seems to check something about your business object(application),so move it to the service layer.You could merge your checkActive()
method logic by moving the checkActive()
method into service layer and calling it inside your applicationService.addApp(application)
as a local method.
2) You have been setting the view name as same in both the if
as well as else
block.Try and move this code out of the if-else block as it becomes redundant.
3) It is a practice to send only the required data from the controller to the view.This is done by creating a DTO(Data Transfer Object) and making a Converter class which maps your business object fields to the DTO.You can look for example use case for DTO here
Upvotes: 1
Reputation: 176
Everything is fine if you are checking for null or incorrect format validation inside your ApplicationFormValidator
Upvotes: 0