How to design domain classes in Grails?

2019-02-15 07:26发布

Given these functional requirements:

User Management

  1. Administrator
  2. Librarian
  3. Borrower

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

Property Management

  1. Book
  2. Memorandum
  3. Circular
  4. License

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{}

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?

1条回答
贪生不怕死
2楼-- · 2019-02-15 07:36

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.

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.

As per your functional requirement:-

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 src\groovy directory as per convention (since the base class is abstract, dependency injection will be defied)

The same goes for Property. Abstract Property class in src\groovy.

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

Summary:-

  • Create grails app

  • Under src\groovy(for example, I am considering a basic structure):

User.groovy:-

abstract class User{
    String name
    String emailId
    OpenID openId
}

Property.groovy:-

abstract class Property{
    String propertyName
}
  • Under grails-app/domain:

Librariran.groovy:-

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

   static mapping = {
   }
}

Book.groovy:-

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

       static mapping = {
       }
}

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.

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

查看更多
登录 后发表回答