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-9/

User Profile Service

Build a user profile management system with dependency injection for data access and email notification services.

Requirements

Implement a system with the following components:

  1. Database Context - A class that simulates database connection and provides methods to fetch and save user data. It should have:

    • get_user(user_id: int) method that returns a dictionary with user data (id, name, email)
    • save_user(user_data: dict) method that persists user data
  2. Email Service - A class that handles email notifications. It should have:

    • send_email(recipient: str, subject: str, body: str) method that simulates sending an email
  3. User Repository - A class that uses the database context to perform user-related data operations. It should:

    • Accept a database context through its constructor
    • Provide a find_user_by_id(user_id: int) method
    • Provide an update_user(user_id: int, name: str, email: str) method
  4. User Profile Service - A class that coordinates user profile operations. It should:

    • Accept both a user repository and email service through its constructor
    • Provide a get_profile(user_id: int) method that retrieves user information
    • Provide an update_profile(user_id: int, name: str, email: str) method that updates user data and sends a confirmation email
  5. Dependency Injection Setup - Configure the system to:

    • Register all components with appropriate lifetimes
    • Use constructor type hints for dependency resolution
    • Build and use the service provider to resolve the UserProfileService

Test Cases

  • Given a registered UserProfileService, when retrieving a profile for user ID 1, then the correct user data is returned. @test
  • Given a registered UserProfileService, when updating a profile with new name and email, then both the repository update and email notification are called. @test
  • Given properly configured dependencies, when building the service provider, then all services can be resolved without errors. @test

Implementation

@generates

API

class DatabaseContext:
    """Simulates database connection and operations."""

    def get_user(self, user_id: int) -> dict:
        """Retrieve user data by ID."""
        pass

    def save_user(self, user_data: dict) -> None:
        """Save user data."""
        pass


class EmailService:
    """Handles email notifications."""

    def send_email(self, recipient: str, subject: str, body: str) -> None:
        """Send an email to the recipient."""
        pass


class UserRepository:
    """Manages user data access."""

    def __init__(self, context: DatabaseContext):
        """Initialize with database context."""
        pass

    def find_user_by_id(self, user_id: int) -> dict:
        """Find and return user by ID."""
        pass

    def update_user(self, user_id: int, name: str, email: str) -> None:
        """Update user information."""
        pass


class UserProfileService:
    """Coordinates user profile operations."""

    def __init__(self, repository: UserRepository, email_service: EmailService):
        """Initialize with repository and email service."""
        pass

    def get_profile(self, user_id: int) -> dict:
        """Retrieve user profile."""
        pass

    def update_profile(self, user_id: int, name: str, email: str) -> None:
        """Update user profile and send confirmation email."""
        pass


def setup_dependency_injection():
    """Configure and return a service provider with all dependencies registered."""
    pass

Dependencies { .dependencies }

rodi { .dependency }

Provides dependency injection support.