Přeskočit na hlavní obsah

Dependency Injection in AI Parsing Module

This section covers the dependency injection (DI) mechanisms used in the AI Parsing module to manage and provide access to the ExtractorFactory across different components of the system.

Overview

The AI Parsing module utilizes dependency injection to ensure that the ExtractorFactory is available wherever it's needed without tight coupling between components. This is achieved through two key constructs:

  • ExtractorFactoryAware Interface: Defines a contract that any class needing access to the ExtractorFactory must implement. It requires implementing a single method setAiExtractorFactory() that accepts an ExtractorFactory instance.

  • ExtractorFactorySetter Trait: Provides a default implementation of the ExtractorFactoryAware interface. Classes can use this trait to quickly gain the ability to store and access an ExtractorFactory instance without writing boilerplate code.

This design pattern is particularly useful for:

  • Maintaining loose coupling between components
  • Making it easy to mock dependencies during testing
  • Ensuring consistent access to the factory throughout the application

Technické informace

ExtractorFactoryAware Interface

  • Namespace: Espo\Modules\AiParsing\Core\Di
  • Method: setAiExtractorFactory(AiExtractorFactory $aiExtractorFactory): void
  • Sets the ExtractorFactory instance that the implementing class will use
  • Parameter: $aiExtractorFactory - An instance of ExtractorFactory

ExtractorFactorySetter Trait

  • Namespace: Espo\Modules\AiParsing\Core\Di
  • Property: protected AiExtractorFactory $aiExtractorFactory
  • Stores the ExtractorFactory instance
  • Method: setAiExtractorFactory(AiExtractorFactory $aiExtractorFactory): void
  • Implements the interface method to set the factory instance
  • Same parameters as the interface method