Where to put enumerations in a Java project?
Asked Answered
D

2

6

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.

Dight answered 24/11, 2013 at 4:2 Comment(3)
I do not believe it really matters as long as you (and your teammates if any) do it consistently.Packston
They are part of your domain, no? (I think you're over analyizing it, though.)Toplevel
By the way, the only correct answer is "on the classpath" ;-)Toplevel
B
5

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).

Blotto answered 24/11, 2013 at 4:48 Comment(2)
Fan, thanks for your informative post but please answer the question I asked. Is it only the size of the model package that made you use domain.types package for types?Dight
Pretty much. Also, with the natures of enums, they tend to be easier to define before the model is even fleshed out, so as it happened many of the types were defined before the model classes were created.Blotto
P
0

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.

Particular answered 7/6, 2017 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.