Skip to content

API Overview

Complete method reference for all public types in Cocoar.Capabilities.

CapabilityScope

csharp
public class CapabilityScope : IDisposable
MemberTypeDescription
CapabilityScope(options?)ConstructorCreate scope with optional configuration
Compose(subject, useRegistry?)MethodCreate a Composer for the subject
Recompose(composition, useRegistry?)MethodCreate a Composer from an existing composition
ComposersPropertyComposerRegistryApi — access the composer registry
CompositionsPropertyCompositionRegistryApi — access the composition registry
OwnerPropertyScopeOwnerApi — owner context management
AnchorsPropertyScopeAnchorsApi — anchors context management
Dispose()MethodClean up registry resources

CapabilityScope<TOwner>

csharp
public class CapabilityScope<TOwner> : CapabilityScope where TOwner : class
MemberTypeDescription
CapabilityScope(owner)ConstructorCreate scope with typed owner
CapabilityScope(owner, options)ConstructorCreate scope with typed owner and options
OwnerPropertyScopeOwnerApi<TOwner> — typed owner API

Composer

csharp
public sealed class Composer
MemberSignatureDescription
SubjectobjectThe subject being composed
AddAdd(object, int?)Add capability with optional order
AddAdd(object, Func<object, int>)Add with order selector
AddAs<TContract>AddAs<T>(object, int?)Add under contract type
AddAs<TContract>AddAs<T>(object, Func<object, int>)Add under contract with order selector
TryAdd<T>TryAdd<T>(T, int?)Add if type not already present
TryAdd<T>TryAdd<T>(T, Func<object, int>)Try-add with order selector
TryAddAs<T>TryAddAs<T>(object, int?)Try-add under contract
TryAddAs<T>TryAddAs<T>(object, Func<object, int>)Try-add under contract with order selector
WithPrimaryWithPrimary(IPrimaryCapability?)Set or clear primary capability
Has<T>boolCheck if capability type exists
HasPrimaryboolCheck if primary is set
RemoveWhereRemoveWhere(Func<object, bool>)Remove matching capabilities
BuildBuild(bool?)Create immutable composition

All mutation methods return Composer for fluent chaining.

IComposition

csharp
public interface IComposition

Properties

PropertyTypeDescription
SubjectobjectThe subject this composition belongs to
TotalCapabilityCountintTotal capabilities across all types

Query Methods

MethodReturnsThrowsDescription
GetAll<T>()IReadOnlyList<T>All capabilities of type
GetAll()IReadOnlyList<object>All capabilities
GetFirstOrDefault<T>()T?First or null
GetLastOrDefault<T>()T?Last or null
GetRequiredFirst<T>()TInvalidOperationExceptionFirst or throw
GetRequiredLast<T>()TInvalidOperationExceptionLast or throw
TryGetFirst<T>(out T)boolTry-pattern first
TryGetLast<T>(out T)boolTry-pattern last
Has<T>()boolHas any of type
Count<T>()intCount of type

Primary Capability Methods

MethodReturnsThrowsDescription
HasPrimary()boolHas any primary
HasPrimary<T>()boolHas primary of type
GetPrimary()IPrimaryCapabilityInvalidOperationExceptionGet primary or throw
GetPrimaryOrDefault()IPrimaryCapability?Get primary or null
TryGetPrimary(out)boolTry-pattern
GetRequiredPrimaryAs<T>()TInvalidOperationExceptionTyped primary or throw
GetPrimaryOrDefaultAs<T>()T?Typed primary or null
TryGetPrimaryAs<T>(out T)boolTyped try-pattern

Using* Extensions

Extension methods on IComposition (namespace: Cocoar.Capabilities).

MethodReturnsDescription
UsingFirst<T>(Action<T>)ICompositionUse first capability (throws if missing)
UsingFirst<T, R>(Func<T, R>)RTransform first capability
UsingFirstOrDefault<T>(Action<T>)ICompositionUse first if exists
UsingLast<T>(Action<T>)ICompositionUse last capability (throws if missing)
UsingLast<T, R>(Func<T, R>)RTransform last capability
UsingLastOrDefault<T>(Action<T>)ICompositionUse last if exists
UsingEach<T>(Action<T>)ICompositionApply to each capability
UsingEach<T, R>(Func<T, R>)IReadOnlyList<R>Transform each capability
UsingAll<T>(Action<IReadOnlyList<T>>)ICompositionUse full collection
UsingAll<T, R>(Func<IReadOnlyList<T>, R>)RTransform full collection

ScopeOwnerApi

MethodReturnsDescription
Set(object)ScopeOwnerApiSet owner (throws if already set)
Replace(object)ScopeOwnerApiReplace owner
Get<T>()T?Get owner or null if not set, collected, or wrong type
GetOrThrow<T>()TGet owner or throw (InvalidOperationException / InvalidCastException)
TryGet<T>(out T?)boolTry-pattern get
Compose(useRegistry?)ComposerCompose on owner
ComposeFor<T>(useRegistry?)ComposerTyped compose on owner
GetComposition()Composition?Get owner's composition
GetRequiredComposition()CompositionGet required or throw
TryGetComposition(out)boolTry-pattern composition
GetCompositionFor<T>()Composition?Typed composition
GetRequiredCompositionFor<T>()CompositionTyped required or throw
GetComposer()Composer?Get owner's composer
GetRequiredComposer()ComposerGet required or throw
TryGetComposer(out)boolTry-pattern composer
GetComposerFor<T>()Composer?Typed composer
GetRequiredComposerFor<T>()ComposerTyped required or throw
ScopeCapabilityScopeReturn to scope

ScopeOwnerApi<TOwner>

MethodReturnsDescription
Get()TOwnerGet typed owner
TryGet(out TOwner?)boolTry-pattern get
Compose(useRegistry?)ComposerCompose on owner
GetComposition()Composition?Get composition
GetRequiredComposition()CompositionRequired or throw
TryGetComposition(out)boolTry-pattern composition
GetComposer()Composer?Get composer
GetRequiredComposer()ComposerRequired or throw
TryGetComposer(out)boolTry-pattern composer
ScopeCapabilityScopeReturn to scope

ScopeAnchorsApi

Anchor Management

MethodReturnsDescription
Set<T>(T)ScopeAnchorsApiSet typed anchor
Set(string, object)ScopeAnchorsApiSet named anchor
Get<T>()T?Get typed anchor or null
GetOrThrow<T>()TGet typed anchor or throw
TryGet<T>(out T?)boolTyped try-pattern
Get(string)object?Get named anchor or null
GetOrThrow(string)objectGet named anchor or throw
TryGet(string, out object?)boolNamed try-pattern
ScopeCapabilityScopeReturn to scope

Composition/Composer Access (typed)

MethodReturnsDescription
ComposeFor<T>(useRegistry?)ComposerCompose on typed anchor
GetCompositionFor<T>()Composition?Get composition
GetRequiredCompositionFor<T>()CompositionRequired or throw
TryGetCompositionFor<T>(out)boolTry-pattern
GetComposerFor<T>()Composer?Get composer
GetRequiredComposerFor<T>()ComposerRequired or throw
TryGetComposerFor<T>(out)boolTry-pattern

Composition/Composer Access (named)

MethodReturnsDescription
Compose(string, useRegistry?)ComposerCompose on named anchor
GetComposition(string)Composition?Get composition
GetRequiredComposition(string)CompositionRequired or throw
TryGetComposition(string, out)boolTry-pattern
GetComposer(string)Composer?Get composer
GetRequiredComposer(string)ComposerRequired or throw
TryGetComposer(string, out)boolTry-pattern

CompositionRegistryApi

MethodReturnsDescription
TryGet<T>(T, out IComposition)boolTry-get by typed subject
TryGet(object, out IComposition)boolTry-get by subject
GetOrDefault<T>(T)IComposition?Get or null
GetOrDefault(object)IComposition?Get or null
GetRequired<T>(T)ICompositionGet or throw
GetRequired(object)ICompositionGet or throw
Remove<T>(T)boolRemove by typed subject
Remove(object)boolRemove by subject

ComposerRegistryApi

MethodReturnsDescription
TryGet<T>(T, out Composer?)boolTry-get composer
GetOrDefault<T>(T)Composer?Get or null
GetRequired<T>(T)ComposerGet or throw
Register<T>(T, Composer, bool)voidRegister composer
Remove<T>(T)boolRemove composer

Other Types

IPrimaryCapability

csharp
public interface IPrimaryCapability { }

Marker interface for primary capabilities.

ISubjectKeyMapper

csharp
public interface ISubjectKeyMapper
{
    bool CanHandle(Type subjectType);
    object Map(object subject);
}

CapabilityScopeOptions

csharp
public record CapabilityScopeOptions
{
    public bool UseComposerRegistry { get; init; } = true;
    public bool UseCompositionRegistry { get; init; } = true;
    public IEnumerable<ISubjectKeyMapper>? SubjectKeyMappers { get; init; }
}

ReadOnlyListExtensions

csharp
public static void ForEach<T>(this IReadOnlyList<T> list, Action<T> action)

Zero-allocation iteration helper.

Thread Safety

TypeThread-Safe?Notes
CapabilityScopeYesRegistries use concurrent collections
ComposerNoUse within a single thread, then Build()
ICompositionYesFully immutable after creation
ScopeOwnerApiYesWeak reference access is thread-safe
ScopeAnchorsApiYesDictionary access is thread-safe

Released under the Apache-2.0 License.