Service Generator

The Service Generator creates service classes that encapsulate business logic and provide a clean interface for your application's core functionality.

Usage

azu generate service SERVICE_NAME [OPTIONS]

Description

Services in Azu applications handle complex business logic, external API integrations, and data processing operations. They provide a clean separation between your application's business rules and the presentation layer.

Options

  • SERVICE_NAME - Name of the service to generate (required)

  • -d, --description DESCRIPTION - Description of the service

  • -m, --methods METHODS - Comma-separated list of methods to generate

  • -t, --template TEMPLATE - Template to use (default: basic)

  • -f, --force - Overwrite existing files

  • -h, --help - Show help message

Examples

Generate a basic service

This creates:

  • src/services/user_service.cr - The service class

  • spec/services/user_service_spec.cr - Test file

Generate a service with specific methods

Generate a service with description

Generated Files

Service Class (src/services/SERVICE_NAME.cr)

Test File (spec/services/SERVICE_NAME_spec.cr)

Service Patterns

Basic Service Pattern

Service with Error Handling

Service with Dependencies

Best Practices

1. Single Responsibility

Each service should have a single, well-defined responsibility:

2. Dependency Injection

Use dependency injection to make services testable:

3. Error Handling

Implement proper error handling and custom exceptions:

4. Method Naming

Use clear, descriptive method names:

Integration with Controllers

Use services in your controllers:

Testing Services

Unit Testing

Mocking Dependencies

Common Service Types

1. CRUD Services

Handle basic Create, Read, Update, Delete operations:

2. External API Services

Handle communication with external APIs:

3. Processing Services

Handle complex data processing:

  • azu generate model - Generate data models

  • azu generate endpoint - Generate API endpoints

  • azu generate contract - Generate validation contracts

  • azu generate middleware - Generate middleware components

Templates

The service generator supports different templates:

  • basic - Simple service with basic structure

  • crud - Service with full CRUD operations

  • api - Service designed for external API integration

  • processing - Service for data processing operations

To use a specific template:

Last updated