Grails 包结构 [英] Grails Package Structure

查看:17
本文介绍了Grails 包结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在选择一个现有的代码库,即 Groovy 和 Grails,但包结构对我来说似乎很奇怪.

I am picking up an existing codebase that is Groovy and Grails, but the package structure seems very odd to me.

对于域类,他们将其放在以下包 com.company.domain 中,然后对于该类的控制器,它是 com.company.controller

For a domain class they put it in the following package com.company.domain then for the controller of that class it is com.company.controller

这种结构对我来说似乎很不合适,因为域和控制器类已经在 grails-app 文件夹中它们自己的文件夹下组织起来了.

That structure seems very off to me since the domain and controller classes are already organized under their own folders in the grails-app folder.

我的计划是根据实际使用情况重做包和组,例如 com.company.billing 和 com.company.util .

My plan is to redo the packages and group based on actual use such as com.company.billing and com.company.util .

我的计划有什么缺点吗?我缺少的当前包结构有什么好处吗?

Are there any disadvantages to my plan? Is there anything good about the current package structure that I'm missing?

推荐答案

我认为包名应该将属于不同业务方面的代码分开.例如一个购物网站,我推荐:

I think package names should separate code belonging to different business aspects. For e.g. a shopping website, I'd recommend:

  • com.mycompany.myfancywebsite.product 到所有与产品相关的内容(例如产品域类、ProductDetailController 等)
  • com.mycompany.myfancywebsite.cart 到所有与购物车相关的内容(例如 CartController、ShippingCostCalculationService 等)
  • com.mycompany.myfancywebsite.payment 到所有与付款相关的事情
  • com.mycompany.myfancywebsite.product to all product related stuff (e.g. Product domain class, ProductDetailController etc.)
  • com.mycompany.myfancywebsite.cart to all shopping cart related stuff (e.g. CartController, ShippingCostCalculationService etc.)
  • com.mycompany.myfancywebsite.payment to all payment related things

恕我直言,使用包名来区分代码的类型"是没有意义的(例如域、控制器、服务......),这根本没有增加任何价值.

IMHO it does not make sense to use package names to distinguish the "type" of code (e.g. domain, controller, service...), this simply adds no value to it.

我还建议在包名中谨慎使用 util,这可能表明您的代码不够集中.

I'd also recommend carefully using util in package names, this might be a sign that your code is not focussed enought.

如需进一步阅读,请参阅优秀书籍清洁代码".另见 http://weblog.dangertree.net/2008/11/22/grails-package-naming/

For further reading, see the excellent book "Clean code". Also see http://weblog.dangertree.net/2008/11/22/grails-package-naming/

这篇关于Grails 包结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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