name: service-registry description: Register and retrieve services at runtime using a thread-safe service registry pattern for loose coupling between plugins.
Plugin Service Registry
When to use this skill
- Register services created by plugins for other plugins to discover
- Retrieve services from the registry in plugin initialization
- Enable loose coupling between plugins via service lookup
- Access core services (User, Account, Session, etc.) in plugins
Key principles
- Lazy discovery: Services retrieved when needed, not all upfront
- Thread-safe: RWMutex allows concurrent reads, exclusive writes
- Type-safe: Use type assertions to convert
anyback to specific interfaces - Named services: Service IDs are constants from models/services.go
Pattern
Services are registered in plugin Init() methods using the registry interface:
// Get a core service
service, ok := ctx.ServiceRegistry.Get(models.ServiceXxx.String()).(ServiceInterface)
if !ok {
return errors.New("service not available")
}
// Register your plugin's service
ctx.ServiceRegistry.Register(models.ServicePlugin.String(), myService)
Always check the ok flag on type assertions. Store retrieved services in plugin struct for internal use.
Service initialization order
- Core repositories created (all depend on database)
- Core services created and registered
- Plugins initialize in order (retrieve core services)
- Plugin services registered as created
- Routes registered after plugins
See bootstrap.go for implementation.
Code references
- Registry interface: models/plugin.go
- Registry implementation: internal/plugins/service_registry.go
- Service IDs: models/services.go
- Example usage: plugins/email-password/plugin.go
Common mistakes
- Not checking
okflag on type assertions - Using string literals instead of
models.ServiceXxxconstants - Registering before implementation complete
- Creating circular dependencies between plugins
- Assuming services are available in wrong initialization phase
Related skills
- plugin-architecture - Plugin lifecycle
- dependency-injection - Wiring core services
- services-and-interfaces - Service patterns