Skip to main content

Backend Architecture

Overview

The OX Agry backend follows a layered architecture pattern with clear separation of concerns:

Directory Structure

src/
├── Controller/ # GraphQL resolvers and controllers
├── Entity/ # Database entities
├── Repository/ # Database repositories
├── Service/ # Business logic services
├── Dto/ # Data transfer objects
├── Event/ # Event listeners and subscribers
├── Exception/ # Custom exceptions
└── Security/ # Authentication and authorization

Core Components

GraphQL Layer

  • Handles API requests using OverblogGraphQLBundle
  • Implements resolvers for queries and mutations
  • Manages data validation and transformation

Business Logic Layer

  • Contains core business rules and workflows
  • Implements service classes for each domain
  • Handles events and notifications

Data Access Layer

  • Manages database operations through repositories
  • Implements caching strategies
  • Handles database transactions

Design Patterns

Repository Pattern

  • Each entity has a dedicated repository
  • Encapsulates database queries
  • Provides data access abstraction
namespace App\Repository;

use App\Entity\Machinery;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

class MachineryRepository extends ServiceEntityRepository
{
public function findAvailableInLocation(string $location): array
{
return $this->createQueryBuilder('m')
->where('m.available = true')
->andWhere('m.location = :location')
->setParameter('location', $location)
->getQuery()
->getResult();
}
}

Service Layer

  • Implements business logic
  • Handles complex operations
  • Manages transactions
namespace App\Service;

class BookingService
{
public function __construct(
private EntityManagerInterface $entityManager,
private EventDispatcherInterface $eventDispatcher
) {}

public function createBooking(BookingData $data): Booking
{
// Implementation
}
}

Authentication Flow

Error Handling

  • Custom exception classes for different error types
  • Consistent error response format
  • Proper error logging and monitoring

Caching Strategy

  • Redis for session storage
  • Query result caching
  • API response caching

Event System

  • Event dispatching for decoupled operations
  • Async event handling for background tasks
  • Event subscribers for cross-cutting concerns

Middleware Pipeline

  1. Authentication
  2. Rate Limiting
  3. Request Validation
  4. GraphQL Processing
  5. Response Formatting

Best Practices

  1. Code Organization

    • Follow PSR-12 coding standards
    • Use typed properties and return types
    • Implement interfaces for major components
  2. Performance

    • Optimize database queries
    • Implement efficient caching
    • Use lazy loading where appropriate
  3. Security

    • Input validation
    • Output sanitization
    • Proper authorization checks
  4. Maintainability

    • Clear documentation
    • Unit tests coverage
    • Consistent naming conventions