Ken Rimple heads Chariot Solutions' training and mentoring programs, and has developed and/or delivered courseware and seminars in a variety of technologies such as Maven, OSGi, Groovy, Grails and Spring. Throughout his career, Ken has always made it a priority to teach others what he has learned. Ken has served as the technical co-chair of both the Fall Forecast 2008 Cloud Computing Conference and the 2009 - 2012 Emerging Technologies for the Enterprise conferences. He hosts a popular podcast, the Chariot TechCast, and has led or participated in projects written in Java since Java 1.0.2. Ken taught the first Philadelphia-area Sun Introduction to Java course in the late 1990s. He is the co-author (along with Srini Penchikala) of Spring Roo in Action for Manning Publications. He is also an avid photographer and jazz drummer. Ken is a DZone MVB and is not an employee of DZone and has posted 35 posts at DZone. You can read more from them at their website. View Full User Profile

Now you can choose ActiveRecord or Service/Repo for Roo 1.2

08.22.2011
| 4916 views |
  • submit to reddit

Check it out!  Now you can install services and repositories, and when you scaffold the project automatically generates calls to the service layer rather than to the Active Record pattern.

I think personally it's a huge win for Roo to be able to support both approaches, since having to switch tools based on a difference in one pattern approach to me feels wrongheaded.  Here's a screenshot from IntelliJ:


Here is a snippet of the code written in the CourseController_Roo_Controller.aj file proving the nice integration:

privileged aspect CourseController_Roo_Controller {
  
  @Autowired
  CourseService CourseController.courseService;
  
  @RequestMapping(method = RequestMethod.POST)
  public String CourseController.create(@Valid Course course, 
       BindingResult bindingResult, Model uiModel, 
       HttpServletRequest httpServletRequest) {
    if (bindingResult.hasErrors()) {
        uiModel.addAttribute("course", course);
        return "courses/create";
    }
    uiModel.asMap().clear();
    courseService.saveCourse(course);
    return "redirect:/courses/" + 
         encodeUrlPathSegment(
            course.getId().toString(), httpServletRequest);
  }
  
  @RequestMapping(params = "form", method = RequestMethod.GET)
  public String CourseController.createForm(Model uiModel) {
      uiModel.addAttribute("course", new Course());
      return "courses/create";
  }
  
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public String CourseController.show(
      @PathVariable("id") Long id, Model uiModel) {
    uiModel.addAttribute("course", courseService.findCourse(id));
    uiModel.addAttribute("itemId", id);
    return "courses/show";
  } 
...
}

To try it out, head over to the nightly builds page and download the latest nightly.  (Do NOT unzip this over a working 1.1 or earlier 1.2 or you'll be crying a sweater of tears).  Then, make your /usr/bin/roo or /usr/bin/roo12 symbolic link (my angle as during the writing of the book I've been testing things against roo 1.1, 1.2, etc).  Create your project and you'll see the options.

You can build this if you put the snapshot repository into your list of repositories in your pom.xml file:

<repository>
    <id>spring-roo-internal-repo</id>
    <name>Spring Roo Internal Snapshot Repo</name>
    <url>http://spring-roo-repository.springsource.org/snapshot</url>
</repository>

Best of luck, and enjoy playing with the upcoming Roo features. I'm also going to check out some newer add-ons, such as JSF and the newly updated Flex (by Roo forum contributor working with the team on a patch) and GWT addons in future blog entries. For now, though, it's back to my add-ons chapter, which is coming soon.

From http://www.rimple.com/tech/2011/8/21/now-you-can-choose-activerecord-or-servicerepo-for-roo-12.html

Published at DZone with permission of Ken Rimple, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mark Unknown replied on Wed, 2011/08/24 - 10:45am

Excellent. I have been wanting this.

Gordon Dickens replied on Wed, 2011/08/31 - 10:29am

Thanks for the posting Ken

Great advance for Roo.

Regards,

Gordon Dickens

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.