Your SlideShare is downloading. ×
0
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Magic with groovy & grails
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Magic with groovy & grails

222

Published on

A introduction to Groovy and Grails. Detailed description of benefits and main components of the framework.

A introduction to Groovy and Grails. Detailed description of benefits and main components of the framework.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
222
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. © Catalysts GmbH Magic with Groovy & Grails The Search is over © Catalysts GmbH
  • 2. What is Groovy? • Dynamic language • It can leverage Java's enterprise capabilities but also has cool productivity features like closures, builders and dynamic typing • 1.0 released on 02.01.2007 • Dynamically compiled to JVM bytecode • 99% of Java code is also valid • Groovy = Java + dynamic typing + additional methods & operators + closures © Catalysts GmbH
  • 3. Dynamic Typing Java ArrayList<String> list = new ArrayList<String>() { { Groovy add("A"); add("B"); add("C"); } }; HashMap<Integer, String> map = new HashMap<Integer, String>() { { put(1, "A"); put(2, "B"); put(3, "C"); } }; © Catalysts GmbH ?
  • 4. Dynamic Method Invocation class Dog { def bark() { println "woof!" } def sit() { println "(sitting)" } def jump() { println "boing!" } } def doAction( animal, action ) { animal."$action"() //action name is passed at invocation } def rex = new Dog() doAction( rex, "bark" ) //prints 'woof!' doAction( rex, "jump" ) //prints 'boing!' © Catalysts GmbH
  • 5. Additional Operators • Spread *. parent*.action //equivalent to: parent.collect{ child -> child?.action } • Elvis ?: assert ['cat', 'elephant']*.size() == [3, 8] • Safe navigation ?. • Equals == © Catalysts GmbH
  • 6. Closures “A Groovy Closure is like a ‘code block’ or a method pointer. It is a piece of code that is defined and then executed at a later point.” http://groovy.codehaus.org/Closures © Catalysts GmbH
  • 7. Functional Programming(1) • Closures as functions def multiply = { x, y -> return x * y } println multiply.call(3, 4) def triple = {x -> return 3*x } • Closures as function compositions //like LISP, Haskell lambda functions def composition = { f, g, x, y-> return f(g(x, y)) } composition.call(triple, multiply, 5, 6) © Catalysts GmbH
  • 8. Functional Programming(2) • Fibonacci def fibonacci fibonacci = { n -> n <= 1 ? n : fibonacci(n-1) + fibonacci(n-2) }.memoize() println fibonacci.call(35) © Catalysts GmbH
  • 9. What is Grails? Grails is a full stack framework and attempts to solve as many pieces of the web development puzzle through the core technology and its associated plugins. Included out the box are things like: 1. An easy to use Object Relational Mapping (ORM) layer built on Hibernate 2. An expressive view technology called Groovy Server Pages (GSP) ~ JSP 3. A controller layer built on Spring MVC 4. A command line scripting environment built on the Groovy-powered Gant 5. An embedded Tomcat container which is configured for on the fly reloading 6. Dependency injection with the inbuilt Spring container 7. Support for internationalization (i18n) built on Spring's core MessageSource concept 8. A transactional service layer built on Spring's transaction abstraction All of these are made easy to use through the power of the Groovy language and the extensive use of Domain Specific Languages (DSLs) © Catalysts GmbH
  • 10. What is Grails? • Web application framework • Convention over configuration • Spring MVC • Fast prototyping and development © Catalysts GmbH
  • 11. Convention over configuration This typically means that the name and location of files is used instead of explicit configuration © Catalysts GmbH
  • 12. © Catalysts GmbH
  • 13. © Catalysts GmbH
  • 14. What does all these means?(1) What does mean all these features of Grails? 1. An easy to use Object Relational Mapping (ORM) layer built on Hibernate That means that you just have to define your domain classes, with all the attributes that you want. These domain classes will be automatically mapped by Hibernate to tables inside the database you have specified in Datasource.groovy, and the domain class attributes will be mapped to table fields. Having this domain classes mapped, you can now perform all CRUD operations on them, with the usage of dynamic methods added by GORM to the domain classes such as “save(), delete(), findAll, findAllBy, findAllWhere, count etc.” These dynamic methods instantly boost your productivity since you dont have to worry anymore about writing sql queries or managing the data layer. Grails does all of these for you! (* however if you still want to customize how things are mapped, sql queries, you can dive into the deep of Hibernate configs) © Catalysts GmbH
  • 15. 2. An expressive view technology called Groovyall these features of Grails? (GSP) Server Pages What does mean Groovy Servers Pages (or GSP for short) is Grails' view technology. It is designed to be familiar for users of technologies such as ASP and JSP, but to be far more flexible and intuitive. You can be more flexible because you can use groovy code inside the pages, that is executed before the page is sent, or use specific reusable componets - taglibs. © Catalysts GmbH
  • 16. 3. A controller layer built on Spring MVC Controllers are the classes that responds and handle requests from the front-end. A controller can generate the response directly or delegate it to a view. Controller methods and views are tightened together, thus a controller method called “search” will by default render the view “search”. © Catalysts GmbH
  • 17. 4. A command line scripting environment built on the Groovy-powered Gant That means that you can use groovy console to run the application, the tests, install plugins and so on. © Catalysts GmbH
  • 18. 5. An embedded Tomcat container which is configured for on the fly reloading Grails provides an embedded Tomcat instance, where your application is deployed automatically when you want it to run. This means that you don't have to worry about a separate Tomcat container and deploy configurations that takes time. Also, you can make changes to your application on the fly, without the need to redeploy the app. However, when changing domain classes, the database will change too, so the application has to be deployed again. © Catalysts GmbH
  • 19. 6. Dependency injection with the inbuilt Spring container Grails leverages Spring MVC in the following areas: Basic controller logic - Grails subclasses Spring's DispatcherServlet and uses it to delegate to Grails controllers Data Binding and Validation - Grails' validation and data binding capabilities are built on those provided by Spring Runtime configuration - Grails' entire runtime convention based system is wired together by a Spring ApplicationContext Transactions - Grails uses Spring's transaction management in GORM In other words Grails has Spring embedded running all the way through it. © Catalysts GmbH
  • 20. 7. Support for internationalization (i18n) built on Spring's core MessageSource This means that your application can be written in as many languages as you want. All you have to do is to use specific codes for each message that you use, and provide different translations for these codes. You can split and organise your language files as you like. © Catalysts GmbH
  • 21. 8. A transactional service layer built on Spring's transaction abstraction The Grails team discourages the embedding of core application logic inside controllers, as it does not promote reuse and a clean separation of concerns. Services in Grails are the place to put the majority of the logic in your application, leaving controllers responsible for handling request flow with redirects and so on. Services are transactional by default. This means that if an operation on the database fails, and this operations is performed in a service, that operation will be automatically rolled back due to default transactional behaviour of services. © Catalysts GmbH
  • 22. Why would I use it? 1- Grails is Spring and Hibernate Grails was a good replacement for vanilla Spring/Hibernate, but it’s even better. In the real world it means that you can bootstrap a new project very quickly by using default settings, but you have all the power of Spring and Hibernate under the hood. If you really need to do some really specific stuff, you can easily go into deep of configurations. The best of both worlds! And it also means that you can use all the Spring modules you already love and integrate with all the databases you are used to, without any problem. You just have to define your domain classes and with the usage of dynamic scaffolding your application is ready to go with all the CRUD operations (views, controllers ) already implemented. © Catalysts GmbH
  • 23. Why would I use it? 2- Groovy is actually Java Java is a statically-typed language and it was designed to be, because it’s just easier to understand. But recently, we have all come to envy the expressivity and power of dynamic languages like Ruby and Python. The problem is that they are completely new languages, which means 2 things: the learning curve is pretty steep, and it’s hard to find good developers for those languages. Well, fear no more, because Groovy is here to save you. Groovy is essentially a superset of Java as a language, which means that you don’t need to learn an entirely new language, and it’s really easy to turn any Java developer into a Groovy developer. Sure Groovy offers much more than Java in terms of power, and it can take some time to get a good grasp of all the ins and outs of closures, dynamic typing or even metaprogramming. But the good news is that you don’t need to master all those right away in order to leverage a lot of the productivity you can get out of Groovy. © Catalysts GmbH
  • 24. Why would I use it? 3- And it runs on the JVM And it gets even further than that: Groovy is so Java-rooted that it actually compiles to run on a Java runtime. Groovy code compiles into JVM bytecode, which means you can still make Groovy and Grails applications run on your favorite application server (simplest is Tomcat, but I’ve made it run on JBoss and even Weblogic) AND you can reuse all the awesome libraries available in the Java ecosystem in an instant. Just plug your environment with your Maven or Ivy repository of choice, or copy some JARs over and you’re good to go! How amazing is that? © Catalysts GmbH
  • 25. Why would I use it? 4- Groovy is not just for scripting I mean sure, Groovy is great for system scripting, in the same way as Ruby and Python are. Not everything needs to be a class so you can write scripts in no time. But a lot of people use Groovy just for that, and think it’s not ready for prime-time big-fuss applications yet Yes, Groovy is a dynamic language, which also means that there is a lot less stuff checked at compilation time than with mere Java, but since your code is much more expressive, there is a lot less opportunity for errors too, and unit tests are a lot easier to write, especially thanks to DSL’s. © Catalysts GmbH
  • 26. Why would I use it? 5- A very dynamic ecosystem Every experienced developer knows that a good framework is nothing without good documentation, serious support and a large community of contributors. Well, of course Groovy and Grails have all that. First of all, they are part of the portfolio of SpringSource, the very company that brought you the Spring Framework and that is now a division of VMWare. The documentation is really good, like most Spring documentations by the way, and there are some excellent books out there. As for helpers and contributors, whether it is on the mailing list or on StackOverflow, helpers are everywhere. And in terms of ecosystem: Grails has a plugin mechanism that makes it really easy to extend the framework in a lot of different ways. Now I won’t lie, all of the available plugins are not necessarily supported or kept up-to-date on the long run, but they are so easy to write and fix, that it’s not really an issue. © Catalysts GmbH
  • 27. Why would I use it? Liquibase integration When you already have your application in production, you need a way to modify your db automatically, thus Liquibase. © Catalysts GmbH
  • 28. Why would I use it? 5- A very dynamic ecosystem Every experienced developer knows that a good framework is nothing without good documentation, serious support and a large community of contributors. Well, of course Groovy and Grails have all that. First of all, they are part of the portfolio of SpringSource, the very company that brought you the Spring Framework and that is now a division of VMWare. The documentation is really good, like most Spring documentations by the way, and there are some excellent books out there. As for helpers and contributors, whether it is on the mailing list or on StackOverflow, helpers are everywhere. And in terms of ecosystem: Grails has a plugin mechanism that makes it really easy to extend the framework in a lot of different ways. Now I won’t lie, all of the available plugins are not necessarily supported or kept up-to-date on the long run, but they are so easy to write and fix, that it’s not really an issue. © Catalysts GmbH

×