Introduce a service registry as Core's composition root
Add Void::Service::ServiceRegistry to Core. initialize() now registers every service by interface — ILogger, ConfigManager, EventBus, INetworkClient, Database, MusicManager, RpcServer — so code can resolve them via Core::services().require<I>() instead of reaching for global singletons. The three former singletons (logger/config/event bus) are wrapped in non-owning aliasing shared_ptrs and remain valid behind the ECHOION_LOG_*/PUBLISH/ SUBSCRIBE macros; the registry just makes them discoverable as services. This establishes the DI backbone; migrating individual call sites to registry resolution is intentionally incremental. Requires the VoidLibrary fix allowing non-polymorphic service types in provide<I>. Link Void::Service. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ff27483ab
commit
32fbffa171
3 files changed
+52
No files matched your search
@@ -107,6 +107,7 @@ target_link_libraries(EchoionCore
|
||||
Void::Math
|
||||
Void::Net
|
||||
Void::Sqlite
|
||||
Void::Service
|
||||
PRIVATE
|
||||
Threads::Threads
|
||||
qjs
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <StateBlob.hpp>
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
@@ -176,6 +177,22 @@ namespace Echoion::Core
|
||||
|
||||
// RPC server for Desktop/API clients
|
||||
std::shared_ptr<RpcServer> rpcServer_;
|
||||
|
||||
// Composition root: a type-keyed registry of all Core services
|
||||
// (logger, config, event bus, network, database, music, rpc).
|
||||
// initialize() provides each instance; resolveServices() and other
|
||||
// code can require<I>() them by interface instead of reaching for
|
||||
// global singletons. The former singletons stay as thin macro
|
||||
// shims over the same instances.
|
||||
Void::Service::ServiceRegistry registry_;
|
||||
|
||||
public:
|
||||
|
||||
// Access the service registry (composition root).
|
||||
[[nodiscard]] Void::Service::ServiceRegistry& services() noexcept
|
||||
{
|
||||
return registry_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
@@ -286,6 +286,40 @@ namespace Echoion::Core
|
||||
setupEventBusSubscriptions();
|
||||
}
|
||||
|
||||
// ── Compose the service registry ─────────────────────────────────
|
||||
// Register every Core service by interface so code can resolve them
|
||||
// via registry_.require<I>() instead of reaching for global singletons.
|
||||
// The three former singletons (logger, config, event bus) are wrapped
|
||||
// in non-owning aliasing shared_ptrs — they remain valid statics behind
|
||||
// the ECHOION_LOG_*/PUBLISH/SUBSCRIBE macros, the registry just makes
|
||||
// them discoverable as services too.
|
||||
{
|
||||
const auto alias = [](auto* p) {
|
||||
using T = std::remove_reference_t<decltype(*p)>;
|
||||
return std::shared_ptr<T>(p, [](T*) {});
|
||||
};
|
||||
if (auto logger = Api::LoggerRegistry::instance().getLogger()) {
|
||||
registry_.provide<Api::ILogger>(logger);
|
||||
}
|
||||
registry_.provide<Api::ConfigManager>(
|
||||
alias(&Api::ConfigManager::instance()));
|
||||
registry_.provide<Api::EventBus>(
|
||||
alias(&Api::GlobalEventBus::instance()));
|
||||
if (networkClient_) {
|
||||
registry_.provide<Api::INetworkClient>(networkClient_);
|
||||
}
|
||||
if (database_) {
|
||||
registry_.provide<Api::Database>(database_);
|
||||
}
|
||||
if (musicManager_) {
|
||||
registry_.provide<MusicManager>(musicManager_);
|
||||
}
|
||||
if (rpcServer_) {
|
||||
registry_.provide<RpcServer>(rpcServer_);
|
||||
}
|
||||
ECHOION_LOG_INFO("Service registry composed");
|
||||
}
|
||||
|
||||
startTime_ = std::chrono::system_clock::now();
|
||||
initialized_ = true;
|
||||
ECHOION_LOG_INFO(std::format("Core initialized (this={})",
|
||||
|
||||
Reference in new issue
Block a user