[PR #24354] refactor: simplify repository factory with Django-style import_string #30591

Closed
opened 2026-02-21 20:47:50 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/24354

State: closed
Merged: Yes


Summary

  • Replaced complex validation logic with Django's simpler import_string pattern
  • Added module caching for better performance
  • Reduced codebase by ~200 lines while maintaining full functionality

Changes

  1. Created libs/module_loading.py: New utility module implementing Django's import_string() function with module caching
  2. Simplified factory classes: Removed validation methods from both core/repositories/factory.py and repositories/factory.py
  3. Removed unnecessary logging: Eliminated debug logging that provided no value
  4. Updated tests: Modified test file to work with simplified implementation

Benefits

  • Simpler code: Removed ~200 lines of complex validation logic
  • Better performance: Module caching prevents redundant imports
  • Proven pattern: Follows Django's battle-tested approach used in production by millions
  • Easier maintenance: Less code to understand and maintain
  • Full compatibility: All existing functionality preserved

Test Plan

  • All existing unit tests pass
  • Repository factory tests updated and passing
  • Code style checks pass (ruff check and ruff format)

Fixes #24353

**Original Pull Request:** https://github.com/langgenius/dify/pull/24354 **State:** closed **Merged:** Yes --- ## Summary - Replaced complex validation logic with Django's simpler `import_string` pattern - Added module caching for better performance - Reduced codebase by ~200 lines while maintaining full functionality ## Changes 1. **Created `libs/module_loading.py`**: New utility module implementing Django's `import_string()` function with module caching 2. **Simplified factory classes**: Removed validation methods from both `core/repositories/factory.py` and `repositories/factory.py` 3. **Removed unnecessary logging**: Eliminated debug logging that provided no value 4. **Updated tests**: Modified test file to work with simplified implementation ## Benefits - **Simpler code**: Removed ~200 lines of complex validation logic - **Better performance**: Module caching prevents redundant imports - **Proven pattern**: Follows Django's battle-tested approach used in production by millions - **Easier maintenance**: Less code to understand and maintain - **Full compatibility**: All existing functionality preserved ## Test Plan - [x] All existing unit tests pass - [x] Repository factory tests updated and passing - [x] Code style checks pass (`ruff check` and `ruff format`) Fixes #24353
yindo added the pull-request label 2026-02-21 20:47:50 -05:00
yindo closed this issue 2026-02-21 20:47:50 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30591