AyCode.Core/AyCode.Models.Server/DynamicMethods/README.md

23 lines
1.7 KiB
Markdown

# DynamicMethods
Reflection-based infrastructure for dynamically dispatching method calls by message tag, primarily used for SignalR message routing.
> **Context:** This is the server-side dispatch engine for the SignalR tag-based architecture.
> See [`docs/SIGNALR_ARCHITECTURE.md`](../../docs/SIGNALR_ARCHITECTURE.md) for the full message flow.
## How It Fits
When `AcWebSignalRHubBase.OnReceiveMessage(tag, bytes, requestId)` is called, the hub uses `DynamicMethodRegistry` to resolve which method to invoke for the given tag. The registry scans all registered service instances for `[SignalR(tag)]` attributed methods, caches the lookup, and invokes the matching method with deserialized parameters.
## Caching Strategy
- **Per-type cache:** `AcDynamicMethodCallModel` builds a `FrozenDictionary<int, AcMethodInfoModel>` once per service type. Immutable after creation.
- **Global cache:** `AcDynamicMethodRegistry` maintains a `ConcurrentDictionary<int, AcMethodInfoModel>` populated lazily per tag on first request.
- **Instance tracking:** The registry maps tags to owning instances for correct invocation context.
## Key Files
- **`AcMethodInfoModel.cs`** — Wraps a `MethodInfo` and its `TagAttribute` with cached parameter metadata (`ParamInfos[]` for deserialization).
- **`AcDynamicMethodCallModel.cs`** — Binds an object instance to its attributed methods, using a static `ConcurrentDictionary` and `FrozenDictionary` cache keyed by message tag. Reflection runs once per type.
- **`AcDynamicMethodRegistry.cs`** — Registry with lazy method lookup across multiple registered instances. Caches discovered methods statically by message tag and resolves instances per request.