|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using Microsoft.AspNetCore.Components;
|
| 5 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
5 | 6 |
|
6 | 7 | namespace Microsoft.Extensions.DependencyInjection;
|
7 | 8 |
|
@@ -50,4 +51,59 @@ public static IServiceCollection AddCascadingValue<TValue>(
|
50 | 51 | public static IServiceCollection AddCascadingValue<TValue>(
|
51 | 52 | this IServiceCollection serviceCollection, Func<IServiceProvider, CascadingValueSource<TValue>> sourceFactory)
|
52 | 53 | => serviceCollection.AddScoped<ICascadingValueSupplier>(sourceFactory);
|
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered |
| 57 | + /// with the value type. This is equivalent to having a fixed <see cref="CascadingValue{TValue}"/> at |
| 58 | + /// the root of the component hierarchy. |
| 59 | + /// </summary> |
| 60 | + /// <typeparam name="TValue">The value type.</typeparam> |
| 61 | + /// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param> |
| 62 | + /// <param name="valueFactory">A callback that supplies a fixed value within each service provider scope.</param> |
| 63 | + /// <returns>The <see cref="IServiceCollection"/>.</returns> |
| 64 | + public static void TryAddCascadingValue<TValue>( |
| 65 | + this IServiceCollection serviceCollection, Func<IServiceProvider, TValue> valueFactory) |
| 66 | + { |
| 67 | + serviceCollection.TryAddEnumerable( |
| 68 | + ServiceDescriptor.Scoped<ICascadingValueSupplier, CascadingValueSource<TValue>>( |
| 69 | + sp => new CascadingValueSource<TValue>(() => valueFactory(sp), isFixed: true))); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered |
| 74 | + /// with the value type, regardless of the <paramref name="name"/>. This is equivalent to having a fixed |
| 75 | + /// <see cref="CascadingValue{TValue}"/> at the root of the component hierarchy. |
| 76 | + /// </summary> |
| 77 | + /// <typeparam name="TValue">The value type.</typeparam> |
| 78 | + /// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param> |
| 79 | + /// <param name="name">A name for the cascading value. If set, <see cref="CascadingParameterAttribute"/> can be configured to match based on this name.</param> |
| 80 | + /// <param name="valueFactory">A callback that supplies a fixed value within each service provider scope.</param> |
| 81 | + /// <returns>The <see cref="IServiceCollection"/>.</returns> |
| 82 | + public static void TryAddCascadingValue<TValue>( |
| 83 | + this IServiceCollection serviceCollection, string name, Func<IServiceProvider, TValue> valueFactory) |
| 84 | + { |
| 85 | + serviceCollection.TryAddEnumerable( |
| 86 | + ServiceDescriptor.Scoped<ICascadingValueSupplier, CascadingValueSource<TValue>>( |
| 87 | + sp => new CascadingValueSource<TValue>(name, () => valueFactory(sp), isFixed: true))); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Adds a cascading value to the <paramref name="serviceCollection"/>, if none is already registered |
| 92 | + /// with the value type. This is equivalent to having a fixed <see cref="CascadingValue{TValue}"/> at |
| 93 | + /// the root of the component hierarchy. |
| 94 | + /// |
| 95 | + /// With this overload, you can supply a <see cref="CascadingValueSource{TValue}"/> which allows you |
| 96 | + /// to notify about updates to the value later, causing recipients to re-render. This overload should |
| 97 | + /// only be used if you plan to update the value dynamically. |
| 98 | + /// </summary> |
| 99 | + /// <typeparam name="TValue">The value type.</typeparam> |
| 100 | + /// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param> |
| 101 | + /// <param name="sourceFactory">A callback that supplies a <see cref="CascadingValueSource{TValue}"/> within each service provider scope.</param> |
| 102 | + /// <returns>The <see cref="IServiceCollection"/>.</returns> |
| 103 | + public static void TryAddCascadingValue<TValue>( |
| 104 | + this IServiceCollection serviceCollection, Func<IServiceProvider, CascadingValueSource<TValue>> sourceFactory) |
| 105 | + { |
| 106 | + serviceCollection.TryAddEnumerable( |
| 107 | + ServiceDescriptor.Scoped<ICascadingValueSupplier, CascadingValueSource<TValue>>(sourceFactory)); |
| 108 | + } |
53 | 109 | }
|
0 commit comments