5.7 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||
|---|---|---|---|---|---|---|---|
|
2026-04-16T04:28:06.522630+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | 30d34b17e2e0aff8 |
ServiceManager
Documentation: ServiceManager Module
1. Purpose
The ServiceManager module implements a lightweight service registry pattern for managing singleton service implementations in the DataPRO core system. It enables components to publish concrete implementations of service interfaces and retrieve them later without tight coupling—publishers and consumers need not know each other’s identities. The module also emits IServicePublishedEvent notifications via the EventManager whenever a service is published or unpublished, supporting reactive service lifecycle monitoring.
2. Public Interface
ServiceManager (static class)
-
void Publish<T>(T item) where T : class
Publishes a singleton service implementationitemfor interface typeT. ThrowsArgumentExceptionifTis already published. Fires aServicePublishedEventwithIsPublished = true. -
void Publish(object item, IEnumerable<Type> interfaceList, bool skipPublishedInterfaces)
Publishesitemfor each interface type ininterfaceList. IfskipPublishedInterfacesisfalse, throwsArgumentExceptionon encountering an already-published interface; otherwise, silently skips it. FiresServicePublishedEventfor each newly published interface. -
bool Exists<T>() where T : class
Returnstrueif an implementation for interface typeTis currently published;falseotherwise. -
bool Exists(Type t)
Overload ofExists<T>using a runtimeTypeinstance. -
T Get<T>() where T : class
Returns the published implementation for interface typeT. ThrowsArgumentExceptionif no implementation is published. -
void Clear<T>() where T : class
Unpublishes the implementation for interface typeT. Fires aServicePublishedEventwithIsPublished = falsebefore removal. -
void Clear(IEnumerable<Type> interfaceList)
Unpublishes all implementations whose interface types are ininterfaceList. FiresServicePublishedEventfor each removed interface.
Note
: All methods are thread-unsafe. No synchronization is applied to the internal
Servicesdictionary.
3. Invariants
- Uniqueness per interface: At most one implementation may be published per interface type (
Type) at any time. Attempting to publish a second implementation for an already-published interface results in anArgumentException, unlessskipPublishedInterfaces = truein the bulkPublishoverload. - Event emission guarantee: Every successful
PublishorClearoperation (i.e., one that modifies the registry) emits exactly oneServicePublishedEventviaEventManager.EventManager.Publish<IServicePublishedEvent>. - No partial failure in bulk operations: In
Publish(object, IEnumerable<Type>, bool), if an exception occurs (e.g., duplicate interface withskipPublishedInterfaces = false), the operation is aborted, and no further interfaces in the list are processed. However, interfaces already processed before the failure remain published (no rollback). - Event payload consistency: The
ServicePublishedEvent.ServiceTypealways matches the interface type being published/unpublished, andIsPublishedreflects the operation direction (truefor publish,falsefor clear).
4. Dependencies
-
Internal dependencies:
System.Collections.Generic.Dictionary<Type, object>for storage.EventManager.EventManager(fromDataPro.Core.EventManager)—used inSendServicePublishedEventto dispatch events.
-
External dependencies:
IServicePublishedEventandServicePublishedEvent(defined in the sameDataPro.Core.ServiceManagernamespace).System.Typefor interface identification.
-
Depended upon by:
- Components that need to register or resolve singleton services (e.g., UI modules, data providers).
- Event subscribers listening to
IServicePublishedEventfor service lifecycle tracking.
5. Gotchas
- No support for multiple implementations per interface: The registry strictly enforces one implementation per interface. Overriding a service requires explicit
Clear<T>()first. - No null-check on
iteminPublish<T>: Passingnullasitemwill succeed (and storenullin the dictionary), leading to aNullReferenceExceptionon subsequentGet<T>()calls. Consider adding validation if nulls are undesirable. - Bulk
Publishdoes not validateitemimplements all requested interfaces: Ifitemdoes not actually implement one or more types ininterfaceList, the dictionary stores the reference, butGet<T>()for that interface will returnnull(viaas T), potentially causing runtime errors later. - No thread safety: Concurrent calls to
Publish,Get, orClearmay corrupt the internal dictionary or cause race conditions (e.g., two threads publishing the same interface may both succeed before the duplicate check completes). - Event emission is synchronous:
SendServicePublishedEventcallsEventManager.EventManager.Publish, which may block the caller until all event handlers complete. Long-running handlers could impact performance. - No versioning or deprecation support: Once a service is published, there is no mechanism to signal obsolescence or migration paths.
None identified from source alone.
(Note: The above gotchas are inferred from code structure and behavior—not assumptions.)