AdapterRegistry class

Singleton registry for managing backend adapters and repository implementations.

The AdapterRegistry provides centralized management of repository implementations from multiple adapters. Unlike the previous version that managed "active adapters", this version works at the repository level - each adapter registers the repositories it provides, and the last registration wins.

Key Features:

  • Singleton Pattern: Only one registry instance exists
  • Factory-Based Registration: Adapters registered via factory functions (lazy initialization)
  • Repository-Level Management: No "active adapter" concept - repositories are registered directly
  • Last Registration Wins: If multiple adapters provide the same repository, last one wins
  • Type Safety: Generic methods for type-safe repository retrieval
  • Mixed Adapters: Can use WooCommerce for products, FCM for notifications, etc.

Example Usage:

// 1. Register adapters (initialization happens at registration time)
final registry = AdapterRegistry();

await registry.registerAdapter(() async {
  final adapter = WooCommerceAdapter();
  await adapter.initialize(config['woocommerce']);
  return adapter;
});

await registry.registerAdapter(() async {
  final adapter = FCMNotificationAdapter();
  await adapter.initialize(config['fcm']);
  return adapter;
});

// 2. Get repositories from anywhere in the app
final productsRepo = AdapterRegistry().getRepository<ProductsRepository>();
final notificationRepo = AdapterRegistry().getRepository<PushNotificationRepository>();

How It Works:

  1. Adapters are registered via factory functions
  2. Each adapter is instantiated and initialized immediately when registered
  3. Repositories are extracted and cached during registration
  4. If two adapters provide the same repository type, the last one wins
  5. Repositories are retrieved by type, no need to know which adapter provides them

Architecture:

  • Plugins depend on repository interfaces (e.g., ProductsRepository)
  • Adapters implement these interfaces for specific platforms
  • Registry manages repository lifecycle and provides access
  • No coupling between plugins and specific adapters

Constructors

AdapterRegistry()
Factory constructor that returns the singleton instance
factory

Properties

adapterCount int
Returns the total number of initialized adapters.
no setter
hashCode int
The hash code for this object.
no setterinherited
isInitialized bool
Checks if adapter registry has been initialized.
no setter
repositoryCount int
Returns the total number of registered repositories.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clearAll() → void
Clears all adapters and repositories (for testing).
getAdapter<T extends BackendAdapter>(String name) → T
Gets a specific adapter by name (advanced usage).
getAvailableRepositories() List<Type>
Returns list of all available repository types.
getInitializedAdapters() List<String>
Returns list of all initialized adapter names.
getRepository<T extends CoreRepository>() → T
Retrieves a repository by type.
hasRepository<T extends CoreRepository>() bool
Checks if a repository type is available.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerAdapter(dynamic factory(), {bool autoInitialize = true}) Future<void>
Registers and initializes a backend adapter.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited