Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Grails is <strong>par-excellence</strong> platform for implementing applications in Domain Driven Design style . At the center of Grails approach are Domain Classes that drive the whole development process. As you are probably guessing, the choice of word domain in Grails is not just a coincidence.</p> <p>You start by defining your Domain Classes and then you can use Grails to do all heavy lifting in providing persistence and generating the GUI. It's worth noting that when the DDD book was written, it was before the Grails or other similar frameworks were created, so a lot of problematic dealt with in a book has to do with issues resolved or greatly reduced by the framework. </p> <h1>Some of DDD concepts resolved by Grails</h1> <p>I will use <a href="http://www.google.cl/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBsQFjAA&amp;url=http://domaindrivendesign.org/sites/default/files/discussion/PatternSummariesUnderCreativeCommons.doc&amp;ei=bThMTqrNB4iitgefocGYCg&amp;usg=AFQjCNF5onwbylXKXK4flanjiad6bWCiHg" rel="noreferrer">DDD pattern summary</a> to address different DDD elements. (Quotes italicized in the text below).</p> <h2>Domain Model</h2> <p>Domain model is structured through Domain classes, Services, Repositories and other DDD Patterns. Let’s take a look at each of these in detail.</p> <h2>Entities</h2> <p><em>“When an object is distinguished by its identity, rather than its attributes, make this primary to its definition in the model”</em></p> <p>These are Domain Classes in Grails. They come with persistence already resolved through GORM. Model can be finely tuned using the GORM DSL. Take a look at hasOne vs. belongsTo property. It can be used to define the lifecycle of entities and their relationships. belongsTo will result in cascading deletes to related entities and other will not. So, if you have a Car object, you can say that Motor “belongsTo” a Car and in that case Car is an Aggregate Root and Motor an aggregate. Note that am I talking here about lifecycle relationship between entities and not the persistence.</p> <h2>Value Objects</h2> <p><em>“When you care only about the attributes of an element of the model, classify it as a VALUE OBJECT. Make it express the meaning of the attributes it conveys and give it related functionality. Treat the VALUE OBJECT as immutable. Don’t give it any identity…”</em></p> <p>In Grails, you can use <a href="http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.2.2Composition" rel="noreferrer">“embedded”</a> property in GORM field to manage a value object. Value object can be accessed only through an entity it belongs to, does not have its own ID and is mapped to same table as the entity it belongs to. Groovy also supports <a href="http://groovy.codehaus.org/Immutable+AST+Macro" rel="noreferrer">@Immutable</a> annotation but I am not sure how it plays with Grails.</p> <h2>Services</h2> <p><em>“When a significant process or transformation in the domain is not a natural responsibility of an ENTITY or VALUE OBJECT, add an operation to the model as a standalone interface declared as a SERVICE. Make the SERVICE stateless.”</em></p> <p>Just like Entities, Services are natively supported in Grails. You place your Grails Service inside the services directory in your Grails project. Services come with following out of the box:</p> <ul> <li>Dependency Injection</li> <li>Transaction Support</li> <li>A simple mechanism for exposing services as web services, so that they can be accessed remotely.</li> </ul> <h2>Modules</h2> <p><em>“Choose MODULES that tell the story of the system and contain a cohesive set of concepts. “</em></p> <p>Grails <a href="http://www.grails.org/doc/latest/guide/12.%20Plug-ins.html" rel="noreferrer">plug-in</a> mechanism provides this and much more: a very simple way to install and create plugins, defines how application can override plugins etc.</p> <h2>Aggregates</h2> <p><em>“Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define boundaries around each. Choose one ENTITY to be the root of each AGGREGATE, and control all access to the objects inside the boundary through the root. Allow external objects to hold references to the root only.”</em></p> <p>I already mentioned some lifecycle control mechanisms. You can use Grails Services and language access control mechanism to enforce access control. You can have a Grails Service playing the role of DDD Repository that permits access to Aggregate Root only. While Controllers in Grails can access GORM operations on Entities directly, I'd argue that for better layered design, Controllers should be injected with services that delegate to GORM Active Record operations.</p> <h2>Factories</h2> <p><em>“Shift the responsibility for creating instances of complex objects and AGGREGATES to a separate object, which may itself have no responsibility in the domain model but is still part of the domain design.”</em></p> <p><a href="http://groovy.codehaus.org/Builders" rel="noreferrer">Groovy builders</a> are excellent alternative for constructing complex objects through rich DSL. In DDD, Factories are more loose term and does not translate directly to <a href="http://en.wikipedia.org/wiki/Design_Patterns" rel="noreferrer">GoF</a> Abstract Factory or Factory Method. Groovy builders are DSL implementation of GoF Builder pattern.</p> <h2>Repositories</h2> <p><em>“For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type. Set up access through a well-known global interface. Provide methods to add and remove objects, which will encapsulate the actual insertion or removal of data in the data store. Provide methods that select objects based on some criteria and return fully instantiated objects or collections of objects whose attribute values meet the criteria, thereby encapsulating the actual storage and query technology. Provide repositories only for AGGREGATE roots that actually need direct access. Keep the client focused on the model, delegating all object storage and access to the REPOSITORIES.”</em></p> <p>Grails Service can be used to implement a dedicated Repository object that simply delegates its operation to Grails GORM. Persistence is resolved with GORM magic. Each Domain class provides a set of dynamic methods that resolve typical CRUD operations including ad-hock querying.</p> <h2>Assertions</h2> <p><em>“State post-conditions of operations and invariants of classes and AGGREGATES. If ASSERTIONS cannot be coded directly in your programming language, write automated unit tests for them.”</em></p> <ul> <li>Take a look at Groovy <a href="https://github.com/andresteingress/gcontracts/wiki/" rel="noreferrer">@Invariant, @Requires, @Ensures</a> annotations, these can be used to declare DbC style Invariants and Pre and Postconditions</li> <li>When you create your domain classes with Grails command line, test classes are created automatically and these are another mechanism for expressing assertions in your domain.</li> </ul> <h2>Declarative Style of Design</h2> <p><em>“A supple design can make it possible for the client code to use a declarative style of design. To illustrate, the next section will bring together some of the patterns in this chapter to make the SPECIFICATION more supple and declarative.”</em></p> <p>This is where Grails excels because of dynamic nature of Groovy language and Builder pattern support for creating custom DSLs.</p> <h2>Layered Architecture</h2> <p>Comes “out-of-the-box” with Grails through proposed “<a href="http://www.grails.org/doc/latest/guide/2.%20Getting%20Started.html#2.6%20Convention%20over%20Configuration" rel="noreferrer">Convention over Configuration</a>” application structure in a form of a layered MVC based implementation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload