Question

We put entities in a domain package. Something like com.acme.domain or com.acme.model.

Entities often have properties which represent codes, rather than free form strings. Something like a sort order code (ASC or DESC) or publish status code (DRAFT, SUBMITTED, PUBLISHED). I create enums for these, so I can make decisions in "business logic".

Question is: where do we put these enums? The same package as entities or a different package like com.acme.domain.support?

When you provide an answer, please explain why you'd put enum in one package vs another.

Was it helpful?

Solution

In the project I am on at work, we defined a "types" package and placed our enums there near our core domain model entities. The types have references to each other in certain cases and are useable on their own as well as with the domain entities, so while I would prefer that they were in the package of the core domain model, it does allow for the core domain model package not to grow too big (the types package has a lot of enums).

For instance, given com.companyname.core.domain.model

We would also have a com.companyname.core.domain.types

package that was used for the core domain types.

The reason for "types" was that we would postfix the enum with "Type" so that it was immediately obvious that it was in fact an enum denoting a particular type of feature. This helps with Eclipse's type search (and probably other IDEs), although I suppose others would state that appending type is redundant given the package name.

We also included ORM mapping classes in the types package to convert to and from database representations (with the types using internal integer codes, not the ordinal, to represent them - just in case someone rearranged them or a constant was no longer needed and should be deleted).

OTHER TIPS

Where to put enumerators is dependent on where they have to be used in the project.

Some use cases :

  1. If we need these enums to be used across systems and multiple services then we can put them either in our clients or domains which are the modules made to be shared.
  2. If we need the use of the enums just within our own service context then we should put it in our service package/module.

So, the cardinal rule is that we should never share the resources or put the resources in sharable modules/packages which are not of any use to clients or the services which are using our shared resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top