Interface IAuthorizedResource<TMessage, TResource>
- Namespace
- Trellis.Authorization
- Assembly
- Trellis.Authorization.dll
Scoped accessor for the resource the framework loaded and authorized for the current pipeline dispatch. Inject this into a handler to obtain the SAME instance the loader returned, eliminating a duplicate load when the handler would otherwise re-fetch the resource by id from its repository.
public interface IAuthorizedResource<TMessage, TResource> where TResource : class
Type Parameters
TMessageThe command or query type this accessor is bound to.
TResourceThe resource type. For commands implementing IAuthorizeResource<TResource> this is the same
TResource. For commands implementing IAuthorizeResourceVia<TOwner> this is the LEAF resource (the thing the message identifies via IIdentifyResource<TResource, TId>), not the via owner — the leaf is the typical mutation target.
- Extension Methods
Remarks
When the accessor is populated. Population happens inside
ResourceAuthorizationBehavior (for direct
IAuthorizeResource<TResource> commands) and inside
ResourceAuthorizationViaBehavior (for via commands — pushes the leaf), and
ONLY after the message's Authorize call returns success. Denied authorizations,
failed loads, and missing actors do NOT populate the accessor.
Identity guarantee. The accessor returns the SAME instance the loader returned. The framework makes no claim about that instance's shape. If your loader returns a projection, a no-tracking EF entity, a stale read-replica POCO, or an HTTP DTO that cannot be mutated and persisted by the handler, do NOT inject this accessor for that command — reload via your repository instead. The framework cannot enforce mutation-readiness; that is the loader author's contract with the handler author.
Concurrency. Implementations are safe to call from nested
mediator.Send and from concurrent Task.WhenAll-style
dispatch of the same closed pair within one DI scope. Implementations use
per-async-flow snapshot semantics so each dispatch sees only its own resource.
Methods
GetRequiredResource()
Returns the resource loaded and authorized for the current pipeline dispatch.
TResource GetRequiredResource()
Returns
- TResource
The loaded resource.
Exceptions
- InvalidOperationException
Thrown when accessed outside a pipeline dispatch that populated the accessor — typically because the message lacks resource-authorization registration, authentication failed, loading failed, authorization was denied, or the handler is being invoked directly (e.g., from a unit test) without going through the mediator pipeline. The exception message names the closed pair so misconfiguration is easy to diagnose.
TryGetResource(out TResource)
Attempts to read the loaded resource. Returns true with the resource when
a pipeline dispatch populated it; returns false when no populated dispatch
is active. Provided for genuinely optional reads (e.g., diagnostic logging that
runs both inside and outside the pipeline). Production handlers should prefer
GetRequiredResource() so a misconfigured pipeline fails loudly instead of
silently skipping work.
bool TryGetResource(out TResource resource)
Parameters
resourceTResourceThe loaded resource on success;
nullotherwise.
Returns
- bool
truewhen populated;falseotherwise.