| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ObservableBehaviors.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Observable.Behaviors; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Facade for registering and accessing behaviors on an <see cref="ObservableObject"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class ObservableBehaviors |
| | | 16 | | { |
| | | 17 | | private readonly BehaviorRegistry _registry; |
| | | 18 | | |
| | 1935 | 19 | | internal ObservableBehaviors(BehaviorRegistry registry) => _registry = registry; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Registers or replaces a behavior for the computed key. |
| | | 23 | | /// </summary> |
| | | 24 | | public void Register<T>(T behavior, string? propertyName = null, string? scope = null) |
| | | 25 | | where T : class, IObservableBehavior |
| | 426 | 26 | | => _registry.Register(behavior, propertyName, scope); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Removes a behavior for the specified key. |
| | | 30 | | /// </summary> |
| | | 31 | | public bool Unregister<T>(string? propertyName = null, string? scope = null) |
| | | 32 | | where T : class, IObservableBehavior |
| | 3 | 33 | | => _registry.Unregister<T>(propertyName, scope); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Tries to get a single behavior matching the optional key parts. |
| | | 37 | | /// </summary> |
| | | 38 | | public bool TryGet<T>([NotNullWhen(true)] out T? behavior, string? propertyName = null, string? scope = null) |
| | | 39 | | where T : class, IObservableBehavior |
| | 138 | 40 | | => _registry.TryGet(out behavior, propertyName, scope); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets all behaviors assignable to <typeparamref name="T"/> matching the optional key parts. |
| | | 44 | | /// </summary> |
| | | 45 | | public T[] GetAll<T>(string? propertyName = null, string? scope = null) |
| | | 46 | | where T : class, IObservableBehavior |
| | 3 | 47 | | => _registry.GetAll<T>(propertyName, scope); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets a single behavior or throws when not found or ambiguous. |
| | | 51 | | /// </summary> |
| | | 52 | | public T Get<T>(string? propertyName = null, string? scope = null) |
| | | 53 | | where T : class, IObservableBehavior |
| | 27 | 54 | | => _registry.Get<T>(propertyName, scope); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Returns whether a single behavior matches the optional key parts. |
| | | 58 | | /// </summary> |
| | | 59 | | public bool Has<T>(string? propertyName = null, string? scope = null) |
| | | 60 | | where T : class, IObservableBehavior |
| | 15 | 61 | | => _registry.Has<T>(propertyName, scope); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets a behavior or <c>null</c> when not found or ambiguous. |
| | | 65 | | /// </summary> |
| | | 66 | | public T? GetOrDefault<T>(string? propertyName = null, string? scope = null) |
| | | 67 | | where T : class, IObservableBehavior |
| | 33 | 68 | | => _registry.GetOrDefault<T>(propertyName, scope); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Executes an action when a single matching behavior exists. |
| | | 72 | | /// </summary> |
| | | 73 | | public bool TryExecute<T>(Action<T> action, string? propertyName = null, string? scope = null) |
| | | 74 | | where T : class, IObservableBehavior |
| | 3 | 75 | | => _registry.TryExecute(action, propertyName, scope); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Executes an action on a single matching behavior. |
| | | 79 | | /// </summary> |
| | | 80 | | public void Execute<T>(Action<T> action, string? propertyName = null, string? scope = null) |
| | | 81 | | where T : class, IObservableBehavior |
| | 3 | 82 | | => _registry.Execute(action, propertyName, scope); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Evaluates a selector when a single matching behavior exists. |
| | | 86 | | /// </summary> |
| | | 87 | | public bool TryEvaluate<TBehavior, TResult>(Func<TBehavior, TResult> selector, [MaybeNullWhen(false)] out TResult re |
| | | 88 | | where TBehavior : class, IObservableBehavior |
| | 3 | 89 | | => _registry.TryEvaluate(selector, out result, propertyName, scope); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Evaluates a selector on a single matching behavior, or returns <paramref name="defaultValue"/>. |
| | | 93 | | /// </summary> |
| | | 94 | | public TResult Evaluate<TBehavior, TResult>(Func<TBehavior, TResult> selector, TResult defaultValue = default!, stri |
| | | 95 | | where TBehavior : class, IObservableBehavior |
| | 3 | 96 | | => _registry.Evaluate(selector, defaultValue, propertyName, scope); |
| | | 97 | | } |
| | | 98 | | |