如何在 Grails 中设计领域类? [英] How to design domain classes in Grails?

查看:12
本文介绍了如何在 Grails 中设计领域类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这些功能要求:

用户管理

  1. 管理员
  2. 图书管理员
  3. 借款人

*用户可以选择通过 OpenID 登录.

*The users have the option of logging-in via OpenID.

物业管理

  1. 预订
  2. 备忘录
  3. 通函
  4. 许可

通常,我会在 Java 中实现这些:

Normally, I would implement these in Java as:

interface User {}
class Librarian implements User {}
class Administrator implements User {}
class Borrower implements User {}

class OpenID {} //all Users HAS AN OpenID attribute (NULL if non-openId login)

interface Property{}
class Book implements Property{}
class Memorandum implements Property{}
class Circular implements Property{}
class License implements Property{}

但是我们的项目将使用 Groovy &Grails,我还没有使用过.我的问题是, 类应该如何根据上述要求设计?我不能使用接口,而且继承似乎不是一个好习惯.我的想法是使用组合,尽管我对将生成的数据库表很困扰.在这种情况下,最佳做法是什么?

But our project will use Groovy & Grails, which I haven't experience using yet. My question is, how should the domain classes be designed based on the requirements above? I can't use an interface, and it seems inheritance is not a good practice. My idea is to use composition, though I'm quite bothered by the database tables that would be generated. What are the best practices in this situation?

推荐答案

首先让我们纠正它,在这种情况下你可以使用 inheritance.您只需要将 has a 关系的约定更改为 is a 关系.

Well first of all lets correct it, you can use inheritance in this case. You just need to change the convention of has a relationship to is a relationship.

需要注意的几个因素:1. Grails 致力于约定优于配置.2. 可以使用GORM封装持久层,借助Hibernate为底层持久层创建Object Mapping.

Few factors to keep note of: 1. Grails works on convention over configuration. 2. You can use GORM which wraps the persistence layer and creates an Object Mapping for the underlying persistence layer with the help of Hibernate.

根据您的功能要求:-

如果您不想将 User 作为持久性的一部分,您可以使用一个 abstractUser 来保存公共属性包括 openId 属性的用户.必须按照约定放在srcgroovy目录下(因为基类是抽象的,依赖注入会被拒绝)

If you do not want to have the User as part of persistence you can have an abstract class User which can hold the common properties of the User including the openId attribute. It has to be placed in srcgroovy directory as per convention (since the base class is abstract, dependency injection will be defied)

Property 也是如此.srcgroovy 中的抽象 Property 类.

The same goes for Property. Abstract Property class in srcgroovy.

现在谈到业务模型,extend abstract 父级中的每个具体实体(domain 类).

Now coming to the business models, extend each of the concrete entities (domain classes) from the abstract parent.

总结:-

  • 创建 grails 应用

  • Create grails app

在srcgroovy下(比如我在考虑一个基本结构):

Under srcgroovy(for example, I am considering a basic structure):

User.groovy:-

User.groovy:-

abstract class User{
    String name
    String emailId
    OpenID openId
}

Property.groovy:-

Property.groovy:-

abstract class Property{
    String propertyName
}

  • grails-app/domain 下:
  • Librariran.groovy:-

    Librariran.groovy:-

    class Librarian extends User{
       //Attributes specific to Librariran
       static constraints = {
       }
    
       static mapping = {
       }
    }
    

    Book.groovy:-

    Book.groovy:-

    class Book extends Property{
        //Attributes specific to Book
           static constraints = {
           }
    
           static mapping = {
           }
    }
    

    依此类推.根据 Grails 约定,grails-app/domain 下的 Groovy 对象被视为具体实体.您显然可以在此处找到更多信息.如果您遇到场景,您也可以使用组合,事实上我已经在 User 中提到了 OpenId.

    So on and so forth. Groovy objects under grails-app/domain are considered concrete entities by Grails convention. More information you can obviously find here. You can also use composition if you come across scenarios, in fact I already mentioned that in User having OpenId.

    注意:- 这是最新版本的 Grails (> 2.x) 的上下文

    Note:- This is context to latest version of Grails (> 2.x)

    这篇关于如何在 Grails 中设计领域类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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