Struct ResourceRef
- Namespace
- Trellis
- Assembly
- Trellis.Core.dll
Identifies a resource by its type and optional identifier. Used as the typed payload for resource-oriented errors such as Error.NotFound, Error.Conflict, Error.Gone, and transport faults that still identify a resource.
public readonly record struct ResourceRef : IEquatable<ResourceRef>
- Implements
- Inherited Members
Constructors
ResourceRef(string, string?)
Identifies a resource by its type and optional identifier. Used as the typed payload for resource-oriented errors such as Error.NotFound, Error.Conflict, Error.Gone, and transport faults that still identify a resource.
public ResourceRef(string Type, string? Id = null)
Parameters
TypestringThe resource type name (e.g.
"User","Order"). Use For<TResource>(object?) when the CLR type name is the desired resource name, or For(string, object?) when a custom domain name is needed. Required.IdstringOptional identifier of the specific resource instance. May be null when the error applies to the resource collection rather than a specific instance.
Properties
Id
Optional identifier of the specific resource instance. May be null when the error applies to the resource collection rather than a specific instance.
public string? Id { get; init; }
Property Value
Type
The resource type name (e.g. "User", "Order"). Use
For<TResource>(object?) when the CLR type name is the desired resource name,
or For(string, object?) when a custom domain name is needed. Required.
public string Type { get; init; }
Property Value
Methods
For(string, object?)
Creates a resource reference from an explicit resource type name and optional identifier.
public static ResourceRef For(string type, object? id = null)
Parameters
Returns
- ResourceRef
A resource reference.
For<TResource>(object?)
Creates a resource reference whose resource type name is derived from
TResource.
public static ResourceRef For<TResource>(object? id = null)
Parameters
idobjectOptional resource identifier.
Returns
- ResourceRef
A resource reference.
Type Parameters
TResourceThe resource type.
Remarks
The resource type name is the simple CLR name with two normalisations applied:
-
Backtick mangling stripped. A closed generic such as
List<User>reports a CLR Name of"List`1"; the backtick suffix is removed so the resource type is"List". Closed generics with multiple type arguments collapse to the outer simple name (e.g.Dictionary<string,int>→"Dictionary"). When the inner type argument is the meaningful resource identifier, prefer For(string, object?) with an explicit name. -
Maybe<T> wrappers are peeled.
Maybe<Order>reports"Order", and the peeling is recursive (Maybe<Maybe<Order>>also reports"Order"). This avoids the CLR-mangled"Maybe`1"from leaking onto the wire when a result type happens to wrap its domain in Maybe<T> (e.g.Result<Maybe<Order>>.ToHttpResponse(...)). Other generic wrappers are not peeled — see FormatTypeName(Type) if you need the formatting without the Maybe-peeling step.
FormatTypeName(Type)
Returns the simple CLR name for type with backtick-mangling
stripped. Used by Trellis components that surface a type-derived identifier on the
wire (e.g. AOT-generated JSON converter fallback messages).
public static string FormatTypeName(Type type)
Parameters
typeTypeThe type whose name should be formatted.
Returns
- string
The simple name with any
`Narity suffix removed. For example,typeof(List<int>)returns"List";typeof(Order)returns"Order".
Remarks
This helper does not peel Maybe<T> wrappers — that is intentionally scoped to For<TResource>(object?), which owns the resource-naming contract.
Exceptions
- ArgumentNullException
Thrown when
typeis null.