Skip to content

refactor(di): extract common validation and error handling patterns#298

Merged
kcenon merged 1 commit into
mainfrom
refactor/issue-297-extract-common-patterns
Jan 22, 2026
Merged

refactor(di): extract common validation and error handling patterns#298
kcenon merged 1 commit into
mainfrom
refactor/issue-297-extract-common-patterns

Conversation

@kcenon

@kcenon kcenon commented Jan 22, 2026

Copy link
Copy Markdown
Owner

Summary

  • Extract duplicated validation patterns from register_factory_internal() and register_instance_internal() into check_frozen_for_registration() and check_already_registered() helper methods
  • Extract duplicated factory exception handling from singleton/transient/scoped resolution branches into invoke_factory_safe() helper method
  • Improve maintainability by centralizing error handling logic

Changes Made

Location Before After
register_factory_internal() Inline frozen check + already registered check (~25 lines) Helper method calls (~8 lines)
register_instance_internal() Inline frozen check + already registered check (~25 lines) Helper method calls (~8 lines)
resolve_with_detection() 3x duplicated try-catch blocks (~27 lines) 3x invoke_factory_safe() calls (~6 lines)

New Helper Methods

// Validates container is not frozen before registration
VoidResult check_frozen_for_registration(
    const std::string& type_name,
    interfaces::registry_action action,
    const std::string& error_message) const;

// Validates service is not already registered (caller must hold lock)
VoidResult check_already_registered(
    std::type_index interface_type,
    const std::string& type_name) const;

// Safely invokes factory with exception handling
Result<std::shared_ptr<void>> invoke_factory_safe(
    const std::function<std::shared_ptr<void>(IServiceContainer&)>& factory);

Test Plan

  • All 111 existing tests pass
  • Build succeeds with no warnings
  • Thread safety preserved (double-checked locking pattern intact)
  • Audit logging behavior unchanged

Closes #297

Extract duplicated validation and error handling code from service_container
into reusable helper methods to improve maintainability:

- Add check_frozen_for_registration() for frozen state validation
- Add check_already_registered() for duplicate registration checks
- Add invoke_factory_safe() for factory exception handling

This eliminates duplicate patterns in:
- register_factory_internal() and register_instance_internal()
- singleton, transient, and scoped resolution branches

All 111 tests pass, thread safety preserved.

Closes #297
@kcenon kcenon merged commit fefeedd into main Jan 22, 2026
24 checks passed
@kcenon kcenon deleted the refactor/issue-297-extract-common-patterns branch January 22, 2026 11:07
kcenon added a commit that referenced this pull request Apr 13, 2026
…298)

Extract duplicated validation and error handling code from service_container
into reusable helper methods to improve maintainability:

- Add check_frozen_for_registration() for frozen state validation
- Add check_already_registered() for duplicate registration checks
- Add invoke_factory_safe() for factory exception handling

This eliminates duplicate patterns in:
- register_factory_internal() and register_instance_internal()
- singleton, transient, and scoped resolution branches

All 111 tests pass, thread safety preserved.

Closes #297
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: Extract common validation and error handling patterns in service_container to eliminate duplication

1 participant