Vaden Framework

repository·main·Indexed 42 days ago

https://github.com/flutterando/vaden

A Spring Boot-inspired backend framework for Dart that implements modern server-side patterns including dependency injection, annotation-based controllers, middlewares, guards, and DTOs. It includes the flutter_vaden addon for creating RESTful API clients using @ApiClient and the vaden_mcp server for AI-driven project introspection and code generation via the Model Context Protocol.

Tokens
30.7K
Snippets
131
Records
161
Agent score
48%

What's inside Vaden

  1. Overview of Vaden Security

    main

    Vaden is a backend framework for Dart inspired by Spring Boot. It is designed to provide a fast and expressive server-side experience by implementing modern backend patterns such as:

    • Annotation-based controllers: For defining API endpoints.
    • Dependency Injection: For managing service lifecycles and components.
    • Middlewares and Guards: For intercepting requests and securing routes.
    • DTOs (Data Transfer Objects): For structured data handling.
    • OpenAPI documentation: For automated API specification.
  2. What is Vaden?

    main

    Vaden is a backend framework for Dart inspired by Spring Boot. It is designed to provide a fast and expressive server-side experience by bringing modern backend concepts to the Dart ecosystem. Key features include:

    • Annotation-based controllers for defining routes and logic.
    • Dependency Injection for managing component lifecycles.
    • Middlewares and Guards for request processing and security.
    • DTOs (Data Transfer Objects) for structured data handling.
    • OpenAPI documentation for automated API specifications.
  3. Overview of Vaden MCP Server capabilities

    main

    The vaden_mcp server allows AI assistants and LLMs to interact with Vaden backend projects via the Model Context Protocol (MCP). It provides tools for:

    • Project Introspection: Scanning and analyzing the project structure.
    • Code Generation: Creating controllers, services, and configurations.
    • Code Mutation: Adding routes and modifying existing components.
    • Context Building: Preparing detailed context specifically for AI-driven code generation.
  4. What is a VadenModule?

    main

    A VadenModule is a class annotated with @VadenModule that serves as the fundamental organizational unit of a Vaden application. Inspired by NestJS and Angular, modules group related components—such as services, controllers, repositories, and other modules—into cohesive blocks of functionality.

    Modules enable:

    • Structured Organization: Clearer application architecture.
    • Dependency Management: Effective use of Vaden's dependency injection (DI) system.
    • Code Reusability: Sharing modules (e.g., a DomainModule) across different parts of an application or between different projects (like backend and frontend).
  5. Built-in complex type serialization

    main

    Vaden provides automatic serialization and deserialization for the following types without requiring custom parsers:

    • DateTime: ISO 8601 strings.
    • Enums: The enum name (e.g., UserType.admin $\rightarrow$ "admin").
    • Duration: Milliseconds.
    • Uri: String representation.
    @DTO()
    class Event {
      final String name;
      final DateTime startTime;     // Automatic ISO 8601
      final Duration duration;      // Automatic milliseconds
      final EventType type;         // Automatic enum name
      final Uri? website;           // Automatic string
    
      const Event(this.name, this.startTime, this.duration, this.type, this.website);
    }
    
    enum EventType { conference, workshop, meetup }
  6. Define Data Transfer Objects (DTOs)

    main

    Use the @DTO() annotation to define Data Transfer Objects. These classes are used for the automatic serialization and deserialization of data in API requests and responses.

    Requirement: All return types in an @ApiClient() and all parameters marked with @Body() must be annotated with @DTO().

  7. Define HTTP routes with @Controller

    main

    A @Controller('/path') defines a class that handles a group of HTTP routes. The path provided to the decorator acts as a prefix for all methods within that class, allowing you to organize your application modularly.

    Constructor injection is supported: if a service is registered in the framework, it can be automatically injected into the controller's constructor.

    @Controller('/users')
    class UserController {
      @Get('/all')
      String getAll() => 'Listing all users';
    }
    // Maps to /users/all
  8. Dependency Injection annotations in flutter_vaden

    main

    Vaden uses several annotations to manage the dependency injection lifecycle:

    • @Configuration(): Defines configuration classes that provide object instances.
    • @Bean(): Marks a method within a @Configuration() class that provides a specific instance to the injector.
    • @Service(): Marks classes as services (prefer registering via interfaces).
    • @Repository(): Marks classes as repositories (prefer registering via interfaces).
    • @Component(): Marks any class that needs to be registered in the dependency injection system.
  9. Implement data access using the Repository Pattern

    main

    Vaden uses the repository pattern to decouple data access logic from the rest of the application. You define an abstract class to establish the contract and a concrete implementation class marked with the @Repository() annotation to provide the actual logic (e.g., using Drift ORM).

    To implement a repository:

    1. Define an abstract class with the required methods.
    2. Create a concrete class that implements the abstract class.
    3. Annotate the concrete class with @Repository().
    4. Inject the database instance (e.g., AppDatabase) into the constructor.
    abstract class TodoRepository {
      Future<List<Todo>> getAll();
      Future<Todo> getById(int id);
      Future<void> add(TodoCreate todo);
    }
    
    @Repository()
    class TodoRepositoryImpl implements TodoRepository {
      final AppDatabase _appDatabase;
    
      TodoRepositoryImpl(this._appDatabase);
    
      @override
      Future<List<Todo>> getAll() async {
        final tables = await _appDatabase.select(_appDatabase.todoTable).get();
        return tables.map(_mapToTodo).toList();
      }
    }
  10. Understand Vaden component lifecycle and resolution

    main

    By default, all registered classes in Vaden are singletons, meaning the same instance is reused throughout the entire application.

    Dependency injection is supported in the following contexts:

    • Controllers
    • Services
    • Repositories
    • Middlewares
    • Guards
    • Beans (via @Bean() methods)
  11. Understand the Vaden project structure

    main

    A standard Vaden project follows a specific directory layout to separate entry points, generated code, configuration, and business logic:

    • bin/server.dart: The application entry point.
    • lib/vaden_application.dart: Generated file (do not edit). It contains the composed application instance.
    • lib/config/: Configuration files, OpenAPI declarations, and settings.
    • lib/src/: Core business logic (controllers, services, repositories, etc.).
    • application.yaml: Global Vaden configuration.
    • pubspec.yaml: Project dependencies.
    • build.yaml: build_runner configuration.
    my_vaden_project/
    ├── bin/
    │   └── server.dart               # Entry point for your server (main file)
    ├── lib/
    │   ├── config/                    # Application configuration files (e.g., OpenAPI, settings)
    │   ├── src/                      # Your actual application code (controllers, services, etc)
    │   └── vaden_application.dart    # Generated file by vaden_class_scanner
    ├── application.yaml              # Vaden configuration
    ├── pubspec.yaml                  # Project dependency manager
    └── build.yaml                    # build_runner configuration