Skip to content

Commit 7ba550c

Browse files
committed
Docs: Add docs / guides for ebean entity bean recommendations
1 parent fff8dfb commit 7ba550c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/guides/entity-bean-creation.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,58 @@ Before writing entity code, remember:
3131

3232
---
3333

34+
## Naming Conventions: The D* (Domain) Prefix Pattern
35+
36+
Entity beans represent internal domain/persistence model details. It's a common best practice in Ebean projects to use the **D* prefix** (D for Domain) for entity class names.
37+
38+
**Why use D* prefix?**
39+
40+
1. **Avoid name clashes with DTOs** - Your public API may have `Customer` (DTO), but your entity is `DCustomer` (Domain). They're clearly different.
41+
2. **Signal intent clearly** - The D prefix immediately tells developers "this is an internal domain class, not part of the public API"
42+
3. **Clarify conversions** - When converting `DCustomer``Customer` (DTO), the direction is obvious
43+
4. **Separate concerns** - API classes in one package (no prefix), domain classes in another (with D prefix)
44+
45+
**Example naming pattern:**
46+
- Entity: `DCustomer`, `DOrder`, `DProduct`, `DInvoice`
47+
- DTO: `Customer`, `Order`, `Product`, `Invoice`
48+
- Converter: `DCustomerMapper.toDTO(DCustomer)``Customer`
49+
50+
**Where to place entities:**
51+
- Entities: `com.example.domain.entity` (or `persistence`)
52+
- DTOs: `com.example.api.model` or `com.example.dto`
53+
54+
**When to use D* prefix:**
55+
-**DO** use for entity beans (internal domain model)
56+
-**DO** use when you have parallel DTO classes with similar names
57+
-**DON'T** use for DTOs or public API classes
58+
-**DON'T** use if you have no DTOs and entities are your public API
59+
60+
Example with and without prefix:
61+
62+
```java
63+
// With D* prefix (recommended - allows both entity and DTO to exist)
64+
@Entity
65+
public class DCustomer {
66+
@Id private long id;
67+
private String name;
68+
// ... entity-specific fields and methods
69+
}
70+
71+
// Public API DTO (no D prefix)
72+
public record Customer(long id, String name) {
73+
// ... conversion method
74+
}
75+
76+
// Conversion
77+
public static Customer toDTO(DCustomer entity) {
78+
return new Customer(entity.getId(), entity.getName());
79+
}
80+
```
81+
82+
This naming convention is optional but highly recommended for projects with separate domain and API layers.
83+
84+
---
85+
3486
## Minimal Entity (No Boilerplate)
3587

3688
This is a complete, valid Ebean entity:

0 commit comments

Comments
 (0)