在开发Grails应用程序时要遵循的最佳实践 [英] Best Practices to be followed while developing Grails Application

查看:117
本文介绍了在开发Grails应用程序时要遵循的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发Grails应用程序时,您认为什么是最佳实践,为什么?我对有关最佳实践的辩论不感兴趣,但是有一个或多个声明以最佳实践何时适用和何时不适用为理由和/或说明进行备份。我不相信开发Grails应用程序有一个最好的方法,但是有很多指导方针可以带来更多可维护的应用程序,并且潜伏的bug更少。



我对Grails的经验是,它提供了如此多的功能,以至于在单个应用程序中都会使用它们,这导致了我在使用GOTO语句调试Fortran程序后看到的一些最差的意大利面代码,并且在部分DO循环之外。

我们都知道Grails如何为域类,服务,视图,控制器等创建一个位置。这些地方?什么规则可以帮助你做正确的事情?什么是Grails代码的气味?解析方案

了解Grails约定。 Grails是公约驱动的;而这些约定就是Grails如何为你做出许多魔术。意见应该是意见。控制器应该只是控制器。服务和模型对象应该包含应用程序的所有逻辑。这意味着当您单击链接或调用端点时,您将调用Controller。控制器将调用一个服务(可能会调用其他服务)并给出一个简洁的响应。该服务将模型对象或数据返回给控制器,该控制器将简单地呈现适当的响应。服务是事务性的,所以任何碰到数据库的东西都应该放在服务中。

  • 测试。测试。测试更多。进行测试是确保您可以添加功能而不会破坏新功能(即您的项目可维护)的最佳方式。


  • 依赖注入。你需要把你的组件放在适当的grails-app /文件夹中。服务进入服务文件夹。控制器进入控制器文件夹。如果你有一个Thing模型对象,并且你需要一个控制器和服务,那么你的控制器和服务应该被命名为ThingController和ThingService。要将ThingService放入ThingController中,请在控制器中放置 def thingService ,然后grails会自动为您安装。如果您未能遵循命名约定和各种组件的放置规则,那么自动装配可能会失败。

  • 了解底层技术,即Spring和Hibernate 。您将遇到建模您的域对象并让它们一起工作的问题。 Grails确实是一个高生产力的框架,但是如果你不明白Hibernate是如何工作的,那么在试图让你的持久性按照你想要的方式行事时,你会很容易迷失。
  • Groovy不是Java。很多Java知识将很好地为您服务。但是Groovy是一种动态语言,Grails有它自己的一套源自Groovy的陷阱。您将在输入Grails时遇到运行时问题,而您在很大程度上避免使用Java。测试对此有帮助。


  • 这些事情似乎很明显,但围绕这些问题出现了许多问题。 b $ b

    While developing a Grails application, what do you consider to be "best practices" and why? I'm not interested in a debate on best practices, but one or more statements backed up with a justification and/or a description of when the best practice applies and when it does not. I don't believe that there is one best way to develop Grails applications but that there are a number of guidelines that will lead to more maintainable applications with fewer bugs lurking in them.

    My experience of Grails is that it offers so many capabilities that there is a temptation to use them all in a single application, which results in some of the worst spaghetti code that I have seen since I debugged a Fortran program with GOTO statements into and out of part of a DO loop.

    We all know how Grails creates a place for domain classes, services, views, controllers, etc. What kind of functions belong in these places? What rules of thumb help you to do the right thing? What are Grails code smells?

    解决方案

    1. Understand the Grails conventions. Grails is convention driven; and the conventions are how Grails can do a lot of its magic for you. Views should just be views. Controllers should just be controllers. Services and Model objects should contain all of the logic of your application. This means the when you click a link, or invoke an endpoint, you call into a Controller. The controller will invoke a service (which might in turn invoke other services) and give a concise response. The service will return model objects or data to the controller, which will simply render an appropriate response. Services are transactional, so anything that hits the database should go in a service.

    2. Test. Test. Test some more. Having tests is the best way to ensure you can add functionality without breaking new functionality, i.e. that your project is maintainable.

    3. Dependency Injection. You need to put your components in the appropriate grails-app/folder. Services go into the services folder. Controllers go into the controllers folder. If you have a Thing model object, and you need a controller and service for it, then your controller and service should be named ThingController and ThingService. To get the ThingService into your ThingController, put def thingService in your controller, and grails will autowire it up for you. If you fail to follow naming conventions and the rules for where to put the various components, the autowiring might fail.

    4. Understand the underlying technologies, namely Spring and Hibernate. You are going to run into issues modeling your domain objects and getting them to work together. Grails really is a high productivity framework, but if you don't understand how Hibernate works, you are going to get lost easily when trying to get your persistence to behave the way you want it.

    5. Groovy is not Java. A lot of Java knowledge will serve you well. But Groovy is a dynamic language and Grails has its own set of gotchas that stem from Groovy. You will run into runtime issues around typing in Grails that you largely avoid with Java. Testing helps with this.

    These things seem obvious, but many questions arise around these issues.

    这篇关于在开发Grails应用程序时要遵循的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆