Skip to content

Modules API

The Modules API provides decorators and utilities for organizing your application into modular components.

Module Decorator

AscModule

__init__

__init__(imports: Sequence[type[AscModuleRef | ControllerRef]], declarations: Sequence[type[T]], providers: MutableSequence[Provider], exports: Sequence[type[T] | str]) -> None

Ascender Module is hierarchy module for handling dependency injection seamlessly.

Parameters:

Name Type Description Default
imports list[type[T]]

Modules to import, providers specified in their exports will be injected into this module

required
declarations list[type[T]]

Declarations, that will be defined in this module.

required
providers list[Provider]

Providers that will supply this module

required
exports list[type[T]]

Exports that will out of encapsulation and will be imported in other module, if this module will be imported to AscModule module

required

create_module

create_module(_parent: type[AscModuleRef] | ControllerRef | type[ControllerRef] | AscenderInjector)

Instantiates module with assigned parent and loads providers.

Parameters:

Name Type Description Default
_parent type[AscModuleRef]

Parent module, by default it should be root

required

Example Usage

Basic Module

from ascender.core import AscModule
from ascender.common import Injectable

@Injectable()
class UserService:
    pass

@Injectable()
class UserRepository:
    pass

@AscModule(
    providers=[UserService, UserRepository],
    declarations=[],
    imports=[],
    exports=[UserService]
)
class UserModule:
    """User management module."""
    pass

Module with Imports

from ascender.core import AscModule
from .database_module import DatabaseModule
from .auth_module import AuthModule

@AscModule(
    imports=[
        DatabaseModule,  # Import database functionality
        AuthModule,      # Import authentication
    ],
    providers=[UserService],
    declarations=[UserController],
    exports=[UserService]  # Export for other modules to use
)
class UserModule:
    """User module with dependencies."""
    pass

Root Application Module

from ascender.core import AscModule
from .user.user_module import UserModule
from .product.product_module import ProductModule
from .auth.auth_module import AuthModule

@AscModule(
    imports=[
        AuthModule,
        UserModule,
        ProductModule,
    ],
    providers=[],
    declarations=[],
)
class AppModule:
    """Root application module."""
    pass

Module with Custom Providers

from ascender.core import AscModule

def create_cache_client():
    from redis import Redis
    return Redis(host='localhost', port=6379)

@AscModule(
    providers=[
        # Class provider
        {
            "provide": UserService,
            "use_class": UserService
        },

        # Factory provider
        {
            "provide": 'CacheClient',
            "use_factory": create_cache_client
        },

        # Value provider
        {
            "provide": 'API_VERSION',
            "value": 'v1'
        },
    ],
    declarations=[UserController],
)
class UserModule:
    """User module with custom providers."""
    pass

Dynamic Module Configuration

from ascender.core import AscModule

class ConfigModule:
    """Dynamic configuration module."""

    @staticmethod
    def forRoot(config: dict):
        """Create module with configuration."""
        return AscModule(
            providers=[
                ValueProvider(provide='Config', useValue=config),
            ],
            exports=['Config']
        )(ConfigModule)

# Usage
@AscModule(
    imports=[
        ConfigModule.forRoot({
            'database_url': 'postgresql://localhost/mydb',
            'api_key': 'secret-key'
        })
    ]
)
class AppModule:
    pass

Module Features

Providers

Define services and dependencies that should be instantiated within the module.

declarations

List of declarations (Controllers or Guards) that belong to this module and handle HTTP requests or any other hooks if defined.

Imports

Other modules whose exported providers should be available in this module.

Exports

Providers that should be available to modules that import this module.

See Also