Class Mixins
Relevant source files
The following files were used as context for generating this wiki page:
- src/flext_core/_mixins_parts/flextmixins_part_01.py (Implicitly referenced via naming conventions and logic)
- src/flext_core/_mixins_parts/flextmixins_part_02.py
- src/flext_core/mixins.py (Implicitly referenced via architecture)
- tests/unit/test_mixins.py
- examples/ex_05_flext_mixins.py
- src/flext_core/_models/_base_parts/flextmodelsbase_part_02.py
- tests/_models/_mixins/init.py
Class Mixins within the FLEXT framework provide a reusable infrastructure for services and domain models to access core system capabilities without manual boilerplate. They serve as a standardized interface for accessing configurations, dependency injection containers, execution contexts, and logging utilities. By composing these mixins, developers can ensure that their classes adhere to the project's architectural standards while gaining built-in observability and lifecycle management.
These mixins are primarily utilized in the service layer to facilitate "context-first" observability and in the domain layer to enforce data consistency and identification. The framework relies on a tiered architecture where mixins are often split into parts (e.g., _part_01, _part_02) to manage complexity and dependencies effectively.
Sources: tests/unit/test_mixins.py:1-12, src/flext_core/_models/_base_parts/flextmodelsbase_part_02.py:1-15
Service Infrastructure Mixins
The FlextMixins class (often re-exposed through the x alias in tests) provides the primary behavioral contract for services. It integrates the four pillars of the FLEXT modernization plan: settings, container, context, and logging.
Core Properties
Services inheriting from these mixins gain access to several read-only properties that satisfy specific protocols:
| Property | Protocol/Type | Description |
|---|---|---|
settings | p.Settings | Access to application settings and feature flags. |
container | p.Container | The Dependency Injection container for service discovery. |
context | p.Context | The active execution context, supporting correlation IDs. |
logger | p.Logger | Structured logging interface for the service. |
Sources: tests/unit/test_mixins.py:38-58
Lifecycle and Registration
Mixins facilitate the registration of services within the global container, making them discoverable by other components. The _init_service method allows a service to register itself, defaulting to its class name if no specific registration name is provided.
This diagram illustrates the idempotent registration flow used by service mixins to ensure discoverability.
Sources: tests/unit/test_mixins.py:100-116
Operation Tracking and Metrics
The track context manager is a significant feature of the mixin infrastructure. It allows services to wrap operations in a tracking block that automatically captures metadata and increments operation counts.
- Success Handling: The context manager yields a metrics mapping containing
operation_name,start_time, andoperation_count. - Error Propagation: Exceptions raised within the
trackblock are propagated, but the mixin ensures that metrics remain consistent, even after a failure. - Idempotency/Persistence: Operation counts are tracked across multiple invocations within the same service instance.
Sources: tests/unit/test_mixins.py:60-94
Domain Model Mixins
Beyond services, the framework utilizes mixins within its Pydantic-based domain models to provide standard behaviors for identity and configuration.
Identifiable Mixin
The IdentifiableMixin provides a standardized unique_id field. It uses UUID v4 by default and ensures that the identifier is part of the model's public schema.
Configuration Mixins
- MutableConfiguredMixin: Enables assignment validation (
validate_assignment=True) for models that require state changes. - NormalizedMutableConfiguredMixin: Extends the mutable mixin by automatically stripping whitespace from string fields (
str_strip_whitespace=True).
Sources: src/flext_core/_models/_base_parts/flextmodelsbase_part_02.py:93-125
Dependency Relationships
The mixins are structured to maintain a strict dependency hierarchy. Tier 0 mixins (like those in FlextModelsBase) depend only on the standard library and Pydantic, while service-level mixins integrate higher-level concerns like the FlextContext.
The class diagram shows the inheritance structure for model mixins and the property resolution for service mixins.
Sources: src/flext_core/_models/_base_parts/flextmodelsbase_part_02.py:100-120, tests/unit/test_mixins.py:38-50
Implementation Example
The following snippet demonstrates the usage of FlextMixins (via the x alias) in a test scenario to verify successful operation tracking.
class _Service(x):
"""Minimal service composing FlextMixins."""
def test_track_yields_metrics(self):
service = self._Service()
with service.track("load") as metrics:
assert "operation_name" in metrics
assert metrics["operation_count"] == 1
Sources: tests/unit/test_mixins.py:31-36, tests/unit/test_mixins.py:60-66
Conclusion
Class Mixins in the FLEXT framework serve as the glue between specialized modules (Settings, Container, Context, Logger) and the application's business logic. By providing standardized properties and lifecycle methods, they enforce clean architecture patterns and significantly reduce the effort required to implement observable, injectable, and identifiable services and models.