Announcement Announcement Module
Collapse
No announcement yet.
Controller generated code redundancy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controller generated code redundancy

    So in class today, I was showing the students some of the .aj controller code while doing the Web MVC and RESTful Web Services sections and someone asked about how validation/exceptions are automatically handled and the form page automatically showing the fields in error.

    So while looking at the code there was some redundancy in methods.

    if (bindingResult.hasErrors()) {
    model.setAttribute("xxx", xxx.getAllxxx());
    more of the same
    }

    And that code would be in 4 or 5 methods.

    First, wouldn't that repetitive code cause many extra calls to the database that aren't needed.

    And secondly, can that code be refactored into just a private method that all the other methods call?

    Thanks

    Mark

  • #2
    Originally posted by bytor99999 View Post
    First, wouldn't that repetitive code cause many extra calls to the database that aren't needed.
    I don't think so, because at most one of those methods is called per request, and each of those methods (createForm, create, updateForm, and update) needs to put the reference data into the model in order to (re)display the form.

    Originally posted by bytor99999 View Post
    And secondly, can that code be refactored into just a private method that all the other methods call?
    Yes, and that's one of the first things I do whenever I need to push those methods into my controller's Java class for any reason (e.g. to filter or sort the reference data in some special way). Having that code in a resuable private method in the AspectJ file would definitely cut down the work involved in pushing those methods in. How about you log it as an improvement?

    Comment


    • #3
      Andrew, please log it as an improvement if it would help you. You're using Roo a lot so if you're finding a regular need to push-in and it would be easy to address we would be quite keen to hear about it.

      Comment


      • #4
        Done

        Logged as ROO-663.

        Comment

        Working...
        X