5.5 KiB
5.5 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||
|---|---|---|---|---|---|---|---|
|
2026-04-16T11:43:09.898125+00:00 | zai-org/GLM-5-FP8 | 1 | a01f75e5b963dd9f |
Documentation: DTS.Common.Core.ServiceManager
1. Purpose
This module implements a service locator pattern via a static ServiceManager class. It allows components to publish implementations of interfaces (services) into a global container and retrieve them elsewhere without direct coupling to the implementation. It acts as a runtime registry for singleton services, supporting publication, retrieval, existence checks, and un-publication, while broadcasting state changes via an event system.
2. Public Interface
IServicePublishedEvent (Interface)
Defines the contract for events fired when a service state changes.
Type ServiceType { get; }— Gets the type of the service being published or unpublished.bool IsPublished { get; }— Returnstrueif the service is being published;falseif it is being unpublished.
ServicePublishedEvent (Class)
Concrete implementation of IServicePublishedEvent.
Type ServiceType { get; internal set; }— The service type. The setter isinternal.bool IsPublished { get; internal set; }— The publication state. The setter isinternal.
ServiceManager (Static Class)
The static gateway for managing service registration.
public static void Publish(T item) where T : class- Registers a service implementation
itemunder the interface typeT. - Throws
ArgumentExceptionifTis already registered.
- Registers a service implementation
public static void Publish(object item, IEnumerable<Type> interfaceList, bool skipPublishedInterfaces)- Registers a single object
itemas the implementation for multiple interfaces defined ininterfaceList. - If
skipPublishedInterfacesisfalse, throwsArgumentExceptionif any interface in the list is already registered. - If
skipPublishedInterfacesistrue, silently skips already registered interfaces.
- Registers a single object
public static bool Exists() where T : class- Returns
trueif a service of interface typeTis currently registered; otherwisefalse.
- Returns
public static bool Exists(Type t)- Non-generic overload. Returns
trueif the specifiedType tis registered; otherwisefalse.
- Non-generic overload. Returns
public static T Get() where T : class- Retrieves the registered service implementation for interface type
T. - Throws
ArgumentExceptionifThas not been published.
- Retrieves the registered service implementation for interface type
public static void Clear() where T : class- Un-registers the service for interface type
T. Fires an "unpublished" event.
- Un-registers the service for interface type
public static void Clear(IEnumerable<Type> interfaceList)- Un-registers all services for the types specified in
interfaceList. Fires "unpublished" events for each removed service.
- Un-registers all services for the types specified in
3. Invariants
- Uniqueness: The
ServiceManagerenforces a one-to-one mapping between aTypeand a service instance. ATypecannot be published twice unless the previous instance is cleared or the batch publish method is used withskipPublishedInterfacesset totrue. - Reference Types Only: All generic methods (
Publish,Exists,Get,Clear) constrain the type parameterTtoclass. - Event Ordering: When clearing a service, the
IServicePublishedEvent(withIsPublished = false) is fired before the service is removed from the internal dictionary. - Exception Consistency: Both
Publish<T>andGet<T>throwArgumentException(not a custom exception type) for invalid states (already exists / not found).
4. Dependencies
- Dependencies (Internal):
ServiceManagerdepends onServicePublishedEventto create event payloads.ServicePublishedEventdepends onIServicePublishedEvent.
- Dependencies (External):
ServiceManagerdepends onEventManager.EventManager. The private methodSendServicePublishedEventcallsEventManager.EventManager.Publish<IServicePublishedEvent>(...). The location/assembly ofEventManageris not defined in the provided source but is required for this module to compile and run.
- Standard Libraries:
System,System.Collections.Generic.
5. Gotchas
- Thread Safety: The internal storage
Dictionary<Type, object> Servicesuses standardSystem.Collections.Generic.Dictionary. There are no locks or synchronization mechanisms inServiceManager. This module is not thread-safe. Concurrent calls toPublish,Get, orClearmay result in race conditions or corruption. - Event Manager Coupling: The
ServiceManageris tightly coupled to a specificEventManager.EventManagerclass. If that event manager is not initialized or accessible, thePublishandClearmethods will fail at runtime when attempting to broadcast events. - Silent Failures in Batch Publish: When calling
Publish(object, IEnumerable<Type>, bool)withskipPublishedInterfacesset totrue, the method will silently ignore interfaces that are already registered. This could lead to stale service instances remaining active if the caller assumed the newitemwould replace them. - Clear Event Timing: Because the "unpublished" event fires before the item is removed from the dictionary, an event subscriber handling
IServicePublishedEventwithIsPublished == falsecould technically still callServiceManager.Exists(type)and receivetrueinside their event handler.