or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/rodi@2.0.x
tile.json

tessl/pypi-rodi

tessl install tessl/pypi-rodi@2.0.0

Implementation of dependency injection for Python 3

Agent Success

Agent success rate when using this tile

92%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.06x

Baseline

Agent success rate without this tile

87%

task.mdevals/scenario-8/

User Profile Service

Build a user profile service that manages user data with proper dependency injection using data classes.

Requirements

Create a user profile service system with the following components:

  1. Configuration Store: A singleton configuration store that holds application settings (database connection string, cache timeout, etc.)

  2. Logger: A simple logging utility that can be shared across services

  3. User Repository: A data class that depends on the configuration store and logger to manage user data persistence

  4. User Service: A data class that depends on the user repository and logger to handle business logic for user operations

  5. Profile Formatter: A data class that formats user profile information for display

The system should:

  • Use data classes for the repository, service, and formatter components
  • Properly register all components with appropriate lifetimes (singleton for config and logger, transient for others)
  • Demonstrate that dependencies are automatically injected into data class fields
  • Include a simple test that verifies the dependency injection works correctly by creating a user service and checking that all nested dependencies are properly initialized

Test Cases

  • Given a properly configured container with all components registered, when resolving UserService, then all its dependencies (repository, logger) should be properly initialized and not None @test
  • Given a UserRepository resolved from the container, when accessing its configuration dependency, then it should be the same singleton instance as directly resolving the configuration @test

Implementation

@generates

API

from dataclasses import dataclass
from typing import Dict, Any

class Configuration:
    """Singleton configuration store"""
    def __init__(self):
        self.settings: Dict[str, Any] = {
            "db_connection": "sqlite:///:memory:",
            "cache_timeout": 300
        }

    def get(self, key: str) -> Any:
        return self.settings.get(key)

class Logger:
    """Simple logging utility"""
    def __init__(self):
        self.logs = []

    def log(self, message: str) -> None:
        self.logs.append(message)

@dataclass
class UserRepository:
    """Manages user data persistence with injected dependencies"""
    config: Configuration
    logger: Logger

    def save(self, user_data: Dict[str, Any]) -> bool:
        self.logger.log(f"Saving user to {self.config.get('db_connection')}")
        return True

@dataclass
class UserService:
    """Handles user business logic with injected dependencies"""
    repository: UserRepository
    logger: Logger

    def create_user(self, name: str, email: str) -> Dict[str, Any]:
        self.logger.log(f"Creating user: {name}")
        user_data = {"name": name, "email": email}
        self.repository.save(user_data)
        return user_data

@dataclass
class ProfileFormatter:
    """Formats user profiles with injected dependencies"""
    logger: Logger

    def format_profile(self, user_data: Dict[str, Any]) -> str:
        self.logger.log("Formatting profile")
        return f"User: {user_data.get('name')} ({user_data.get('email')})"

def setup_container():
    """
    Configure and return a container with all components properly registered.
    Returns a built provider ready to resolve services.
    """
    pass

Dependencies { .dependencies }

rodi { .dependency }

Provides dependency injection support with dataclass integration.

@satisfied-by