<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Random Musings&#187; mvc</title> <atom:link href="http://www.nslms.com/tag/mvc/feed/" rel="self" type="application/rss+xml" /><link>http://www.nslms.com</link> <description></description> <lastBuildDate>Wed, 06 Jul 2011 20:47:34 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>Modularizing your Grails Application &#8211; Domain Classes</title><link>http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link> <comments>http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/#comments</comments> <pubDate>Thu, 11 Mar 2010 01:39:07 +0000</pubDate> <dc:creator>RyanG</dc:creator> <category><![CDATA[Grails/Groovy]]></category> <category><![CDATA[Reviews]]></category> <category><![CDATA[grails]]></category> <category><![CDATA[groovy]]></category> <category><![CDATA[linkedin]]></category> <category><![CDATA[modular]]></category> <category><![CDATA[mvc]]></category> <category><![CDATA[reusable]]></category> <guid
isPermaLink="false">http://www.nslms.com/?p=417</guid> <description><![CDATA[This is the second installment of my What Grooves You? series of posts, this time discussing how to modularize your Grails application. While Grails does an awesome job of enforcing MVC once your application reaches a certain size, or you have multiple applications which may have shared components, you&#8217;re going to have to start thinking [...]]]></description> <content:encoded><![CDATA[<div
class="tweetmeme_button" style="float: right; margin-left: 10px;"> <a
href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.nslms.com%2F2010%2F03%2F10%2Fmodularizing-your-grails-application-domain-classes%2F"><br
/> <img
src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.nslms.com%2F2010%2F03%2F10%2Fmodularizing-your-grails-application-domain-classes%2F&amp;source=rjgeyer&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br
/> </a></div><p>This is the second installment of my <a
href="http://www.nslms.com/2010/02/05/what-grooves-you/">What Grooves You?</a> series of posts, this time discussing how to modularize your Grails application.  While Grails does an awesome job of enforcing <a
href="http://www.grails.org/Developer+-+Spring+MVC+Integration">MVC</a> once your application reaches a certain size, or you have multiple applications which may have shared components, you&#8217;re going to have to start thinking about how your going to modularize the reusable parts of your code.</p><p><span
id="more-417"></span></p><p>I encountered this pretty quickly because the application I am working on is broken out into two distinct parts, a public facing web form for submitting data (the &#8220;Front End&#8221;) and a private back office application for managing those user submissions (the &#8220;Back End&#8221;).  These two parts of the application (for compliance reasons) cannot run on the same internet facing system, and the &#8220;Back End&#8221; must only be available on the internal network.  Of course, both of these applications are going to work with the same database and therefore the same Domain classes.  It would be dangerous and tedious to try to keep the separate domain classes in each project synchronized with one another.</p><h2>What won&#8217;t work</h2><p><b>The RESTful JSON Service</b></p><p>My first thought was to deploy a 3rd application which would be internet visible and act as a proxy for all the database requests.  I could then query that application with REST and handle a JSON payload which would be my domain object.  This actually seemed pretty elegant since I wouldn&#8217;t have to actually share any code between the Front End and Back End applications and I still got a well defined object on either end.  The problem of course is that all I get is the data for my domain class, and I don&#8217;t have access to any of the functionality that GORM gives me &#8220;for free&#8221;.  I&#8217;d have to duplicate search functionality, limits, grouping, sorting, and all sorts of other querying tools in my service.  That seemed like an awful lot of work for functionality that is offered by GORM and works very well!</p><p><b>Just JAR it man</b></p><p>The next obvious conclusion is to just toss my domain classes into a library JAR file and reference that library in both of my other applications.  This way I actually have the whole domain class and access to the dynamic find methods and all that other good stuff.  But, how do you package these?  Do you compile the Groovy classes then package the .class files? Will the data source information have to be set for the domain class(es) in the JAR, or will the data source of the application referencing the library be used?</p><p>Now some more seasoned Java and Hibernate developers might simply laugh at that barrage of questions, but for me it presented a serious barrier to entry.  Fortunately there is a better way.</p><h2>Just plug it in!</h2><p>It didn&#8217;t take me long to discover that putting my reusable code into a <a
href="http://grails.org/doc/latest/guide/12.%20Plug-ins.html" target="_blank">Grails Plugin</a> was the best and most scalable approach. For the sake of demonstration I&#8217;m going to take you through an example comment submission and administration application, kinda like blog comments.</p><p><b>The plugin project</b></p><p>First, let&#8217;s go ahead and create our plugin project.</p><p>[bash]grails create-plugin Modular-DAL[/bash]</p><p>Once you&#8217;ve got your shiny new plugin created, open it up with your favorite IDE (I use <a
href="http://www.springsource.com/products/sts">Spring Source Tool Suite</a>) and add a new domain class that you&#8217;re going to be sharing.</p><p>[bash]grails create-domain-class com.nslms.modular.domain.Comment[/bash]</p><p>Now we specify some properties for our new shared domain class.</p><p
class="filename">Comment.groovy</p><p>[groovy]<br
/> package com.nslms.modular.domain</p><p>class Comment {</p><p> static constraints = {<br
/> name(blank:false)<br
/> email(blank:false)<br
/> comment(blank:false)<br
/> }</p><p> String name<br
/> String email<br
/> String website<br
/> String comment</p><p> Boolean isApproved = false<br
/> }<br
/> [/groovy]</p><p>With our new shared domain class created, we want to package up our plugin so we can load it into the other projects which we&#8217;ll be creating in a moment.</p><p>[bash]grails package-plugin[/bash]</p><p>That&#8217;s it, you&#8217;ve just created a (very small) module of your application which contains a shared domain class.  This could, of course, contain any number of domain classes, controllers, or services, views, javascript, css, etc. that would be used by other parts of your application, or by other applications.</p><p><b>The Front End</b></p><p>Now, lets create an application which will serve as the &#8220;front end&#8221; or externally facing form for collecting data.</p><p>[bash]grails create-app Modular-FrontEnd[/bash]</p><p>Then the very important part of installing the plugin we just created</p><p>[bash]grails install-plugin ../Modular-DAL/grails-modular-dal-0.1.zip[/bash]</p><p>Because you can <a
href="#download-instructions">download</a> the project I created, I&#8217;m not going to go into excruciating detail about the controller and view(s) I setup in my front end, but sufficed to say I am accessing the &#8220;Comment&#8221; domain class that is supplied by the Modular-DAL plugin project!</p><p
class="filename">CommentsController.groovy (snippet)</p><p>[groovy highlight="1,5"]<br
/> import com.nslms.modular.domain.*</p><p>class CommentsController {</p><p> def index = { [comments: Comment.findAllByisApproved(true)] }<br
/> }<br
/> [/groovy]</p><p>The result of the front end app should be a list of comments which are approved (by the backend) and a submission form to allow you to submit new comments.  Kinda like <a
href="http://www.nslms.com/grails/examples/modular/frontend/comments" target="_blank">this</a>.</p><p><b>The Back End</b></p><p>Now we need to create the system which will allow you as the administrator to approve the comments submitted by the unwashed masses.</p><p>[bash]grails create-app Modular-BackEnd[/bash]</p><p>And install the plugin with the shared domain class.</p><p>[bash]grails install-plugin ../Modular-DAL/grails-modular-dal-0.1.zip[/bash]</p><p>Again because you can <a
href="#download-instructions">download</a> the project I created, here&#8217;s just a snippet of the admin controller showing the juicy bits where we use the shared domain class</p><p
class="filename">AdminController.groovy (snippet)</p><p>[groovy highlight="1,5"]<br
/> import com.nslms.modular.domain.*;</p><p>class AdminController {</p><p> def comments = { [comments: Comment.findAllByisApproved(false)] }<br
/> }<br
/> [/groovy]</p><p>The back end app should have a list of all unapproved comments, and a method to approve them.  Kinda like <a
href="http://www.nslms.com/grails/examples/modular/backend/admin/comments" target="_blank">this</a>.</p><p><b>Trying it out</b></p><p>Now if you&#8217;ve followed along and created your own controllers and views, or <a
href="#download-instructions">downloaded</a> my basic project, you&#8217;re going to want to try running both the front end and back end at the same time, persisting data to a common datasource so that you can see the whole thing in action.  If you just use the grails run-app command, you&#8217;ll find very quickly that you can only run one or the other project, but not both at the same time.  This is because they&#8217;ll both be trying to run on the common Tomcat port (8080).  To overcome this, and run both apps at the same time, try the following starting from the Modular-FrontEnd directory.</p><p>[bash]<br
/> grails -Dserver.port=8081 run-app<br
/> cd ../Modular-BackEnd<br
/> grails -Dserver.port=8082 run-app<br
/> [/bash]</p><p>Now you should be able to access both applications at <a
href="http://localhost:8081/FrontEnd">http://localhost:8081/FrontEnd</a> and <a
href="http://localhost:8082/BackEnd">http://localhost:8082/BackEnd</a> respectively.</p><p><b
id="download-instructions">Download The Project(s)</b></p><p>If you want to download the project(s) and follow along, fire up your favorite subversion client and export everything at <del
datetime="2010-12-26T22:17:49+00:00"><a
href="svn://linode.nslms.com/blog/grails/Modular">svn://linode.nslms.com/blog/grails/Modular</a></del> or download it <a
href="http://www.nslms.com/downloads/Modular.zip">here</a>.  A couple things to note if you&#8217;re grabbing my project, it&#8217;s currently setup to use a MySQL database named &#8220;modular&#8221; running on the same system as the application.  If you don&#8217;t already have MySQL setup, give <a
href="http://www.apachefriends.org/en/xampp.html">XAMPP</a> a look to get you started quickly.  Also, I didn&#8217;t include the JDBC driver, go fetch it <a
href="http://www.mysql.com/downloads/connector/j/">here</a> and drop it into the &#8220;lib&#8221; directory of both the FrontEnd and BackEnd applications.  Lastly, these projects are all written with Grails 1.2.1 so you&#8217;ll have to be using 1.2.1 or newer.</p><p><em><strong><span
style="color: #ff0000;">* UPDATE: The example apps have a new home..</span></strong></em></p><p>Grab the projects at</p><p>[bash]<br
/> git clone git://ec2.nslms.com/grails/blog_example_modular<br
/> [/bash]</p><div
class="shr-bookmarks shr-bookmarks-center"><ul
class="socials"><li
class="shr-blogger"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=219&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a></li><li
class="shr-comfeed"> <a
href="http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-linkedin"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a></li><li
class="shr-mail"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing%20your%20Grails%20Application%20-%20Domain%20Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li><li
class="shr-posterous"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=210&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this to Posterous">Post this to Posterous</a></li><li
class="shr-twitter"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%2524%257Btitle%257D%2B-%2B%2524%257Bshort_link%257D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-digg"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-friendfeed"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=43&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a></li><li
class="shr-googlereader"> <a
href="http://www.shareaholic.com/api/share/?title=Modularizing+your+Grails+Application+-+Domain+Classes&amp;link=http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/&amp;notes=This%20is%20the%20second%20installment%20of%20my%20What%20Grooves%20You%3F%20series%20of%20posts%2C%20this%20time%20discussing%20how%20to%20modularize%20your%20Grails%20application.%20%20While%20Grails%20does%20an%20awesome%20job%20of%20enforcing%20MVC%20once%20your%20application%20reaches%20a%20certain%20size%2C%20or%20you%20have%20multiple%20applications%20which%20may%20have%20shared%20components%2C&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a></li></ul><div
style="clear: both;"></div><div
class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a
target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div
style="clear: both;"></div></div> ]]></content:encoded> <wfw:commentRss>http://www.nslms.com/2010/03/10/modularizing-your-grails-application-domain-classes/feed/</wfw:commentRss> <slash:comments>30</slash:comments> </item> </channel> </rss>
