admin管理员组

文章数量:1394610

I am developing a web application and want to implement base36-encoded IDs for URLs, similar to Reddit's approach (e.g., reddit/r/programming/comments/1jkgh2s/ where 1jkgh2s is the base36-encoded ID).

For efficiency, I plan to store IDs as integers in the database. However, I'm unsure which architectural layer should be responsible for the base36 encoding/decoding:

  1. Presentation Layer: Convert in HTTP handlers

    • Store IDs as integers in database
    • Use integer IDs in domain model
    • Convert between integer and base36 when encoding/decoding URLs
  2. Application Layer: Convert in services

    • Store IDs as integers in database
    • Use integer IDs in domain model
    • Convert between integer and base36 when interacting with lower layers (domain and infrastructure)
    • Presentation and application layers use base36 IDs, all other layers use integer IDs
  3. Domain Layer: Convert in domain model

    • Store IDs as integers in database
    • Use base36 IDs in domain model
    • Convert between integer and base36 when hydrating domain objects
    • All layers use base36 IDs
  4. Infrastructure Layer: Convert in data access

    • Store IDs as integers in database
    • Use base36 IDs in domain model
    • Convert between integer and base36 during data retrieval
    • All layers use base36 IDs

Approaches 2, 3, and 4 provide consistent base36 ID contract for all presentation layers (Web, API, CLI).

Additional context:

  • Planning to add more interfaces (e.g., RESTful API, CLI) in the future
  • Deliberately choosing not to use UUIDs in favor of shorter, more human-friendly identifiers
  • Aware of the security implications of using auto-incrementing IDs (whether encoded in base36 or not) and have accepted these trade-offs

Key Questions:

  1. Which layer is most appropriate for this responsibility?
  2. Should all interfaces (Web, API, CLI) use base36 IDs for consistency?
  3. If all interfaces should use base36, does this suggest it belongs in the domain layer?

I am developing a web application and want to implement base36-encoded IDs for URLs, similar to Reddit's approach (e.g., reddit/r/programming/comments/1jkgh2s/ where 1jkgh2s is the base36-encoded ID).

For efficiency, I plan to store IDs as integers in the database. However, I'm unsure which architectural layer should be responsible for the base36 encoding/decoding:

  1. Presentation Layer: Convert in HTTP handlers

    • Store IDs as integers in database
    • Use integer IDs in domain model
    • Convert between integer and base36 when encoding/decoding URLs
  2. Application Layer: Convert in services

    • Store IDs as integers in database
    • Use integer IDs in domain model
    • Convert between integer and base36 when interacting with lower layers (domain and infrastructure)
    • Presentation and application layers use base36 IDs, all other layers use integer IDs
  3. Domain Layer: Convert in domain model

    • Store IDs as integers in database
    • Use base36 IDs in domain model
    • Convert between integer and base36 when hydrating domain objects
    • All layers use base36 IDs
  4. Infrastructure Layer: Convert in data access

    • Store IDs as integers in database
    • Use base36 IDs in domain model
    • Convert between integer and base36 during data retrieval
    • All layers use base36 IDs

Approaches 2, 3, and 4 provide consistent base36 ID contract for all presentation layers (Web, API, CLI).

Additional context:

  • Planning to add more interfaces (e.g., RESTful API, CLI) in the future
  • Deliberately choosing not to use UUIDs in favor of shorter, more human-friendly identifiers
  • Aware of the security implications of using auto-incrementing IDs (whether encoded in base36 or not) and have accepted these trade-offs

Key Questions:

  1. Which layer is most appropriate for this responsibility?
  2. Should all interfaces (Web, API, CLI) use base36 IDs for consistency?
  3. If all interfaces should use base36, does this suggest it belongs in the domain layer?
Share Improve this question asked Mar 27 at 11:17 BARJBARJ 2,0223 gold badges24 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

​In a web application, base436 ID conversion is typically handled in the presentation layer. This is where integer IDs are encoded or decoded for a URL-friendly representation. This approach maintains internal consistency with integer IDs while presenting user-friendly URLs. However, the optimal layer may vary based on specific architectural requirements and design preferences.

本文标签: encodingWhich component or layer should be responsible for base36 ID conversionStack Overflow