| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FeaturesExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Metadata; |
| | | 9 | | using MyNet.Observable.Behaviors.Metadata; |
| | | 10 | | using MyNet.Observable.Behaviors.Metadata.Features; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Observable; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Fluent configuration extensions for observable metadata. |
| | | 18 | | /// Authoring API: <see cref="MetadataRegistry.For{T}"/> → <see cref="PropertyBuilder{T,TProperty}"/>. |
| | | 19 | | /// Batch API: <see cref="TypeMetadata"/> extension methods for multiple property names. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class FeaturesExtensions |
| | | 22 | | { |
| | | 23 | | extension(TypeMetadata metadata) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Configures the specified properties to refresh when the application culture changes. |
| | | 27 | | /// </summary> |
| | | 28 | | public TypeMetadata UpdateOnCultureChanged(params string[] propertyNames) |
| | | 29 | | { |
| | 18 | 30 | | foreach (var propertyName in propertyNames) |
| | 6 | 31 | | MetadataApplicators.ApplyUpdateOnCultureChanged(metadata.GetProperty(propertyName)); |
| | | 32 | | |
| | 3 | 33 | | return metadata; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Configures the specified properties to refresh when the application time zone changes. |
| | | 38 | | /// </summary> |
| | | 39 | | public TypeMetadata UpdateOnTimeZoneChanged(params string[] propertyNames) |
| | | 40 | | { |
| | 0 | 41 | | foreach (var propertyName in propertyNames) |
| | 0 | 42 | | MetadataApplicators.ApplyUpdateOnTimeZoneChanged(metadata.GetProperty(propertyName)); |
| | | 43 | | |
| | 0 | 44 | | return metadata; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Configures the specified properties to ignore modification tracking. |
| | | 49 | | /// </summary> |
| | | 50 | | public TypeMetadata IgnoreModificationTracking(params string[] propertyNames) |
| | | 51 | | { |
| | 12 | 52 | | foreach (var propertyName in propertyNames) |
| | 3 | 53 | | MetadataApplicators.ApplyIgnoreModificationTracking(metadata.GetProperty(propertyName)); |
| | | 54 | | |
| | 3 | 55 | | return metadata; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | extension<T, TProperty>(PropertyBuilder<T, TProperty> builder) |
| | | 60 | | { |
| | | 61 | | /// <summary> |
| | | 62 | | /// Configures the property to react to changes of the specified event type. |
| | | 63 | | /// Prefer <see cref="UpdateOnCultureChanged"/> / <see cref="UpdateOnTimeZoneChanged"/> for built-in events. |
| | | 64 | | /// </summary> |
| | | 65 | | public PropertyBuilder<T, TProperty> ReactTo(Type eventType) |
| | | 66 | | { |
| | 3 | 67 | | ArgumentNullException.ThrowIfNull(eventType); |
| | 3 | 68 | | var feature = builder.Metadata.GetOrCreate<EventReactionFeature>(); |
| | 3 | 69 | | feature.Events.Add(eventType); |
| | 3 | 70 | | return builder; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Configures the property to update when the culture changes. |
| | | 75 | | /// </summary> |
| | | 76 | | public PropertyBuilder<T, TProperty> UpdateOnCultureChanged() |
| | | 77 | | { |
| | 9 | 78 | | MetadataApplicators.ApplyUpdateOnCultureChanged(builder.Metadata); |
| | 9 | 79 | | return builder; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Configures the property to update when the time zone changes. |
| | | 84 | | /// </summary> |
| | | 85 | | public PropertyBuilder<T, TProperty> UpdateOnTimeZoneChanged() |
| | | 86 | | { |
| | 6 | 87 | | MetadataApplicators.ApplyUpdateOnTimeZoneChanged(builder.Metadata); |
| | 6 | 88 | | return builder; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Configures the property to ignore modification tracking. |
| | | 93 | | /// </summary> |
| | | 94 | | public PropertyBuilder<T, TProperty> IgnoreModificationTracking() |
| | | 95 | | { |
| | 9 | 96 | | MetadataApplicators.ApplyIgnoreModificationTracking(builder.Metadata); |
| | 9 | 97 | | return builder; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Configures the property to relay child property-changed notifications to the owner. |
| | | 102 | | /// </summary> |
| | | 103 | | public PropertyBuilder<T, TProperty> ForwardPropertyChanged(bool concatenatePropertyName = true) |
| | | 104 | | { |
| | 3 | 105 | | MetadataApplicators.ApplyForwardProperty(builder.Metadata, concatenatePropertyName); |
| | 3 | 106 | | return builder; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Configures the property to validate when the specified dependent properties change. |
| | | 111 | | /// </summary> |
| | | 112 | | public PropertyBuilder<T, TProperty> Validates(params string[] dependentProperties) |
| | | 113 | | { |
| | 24 | 114 | | foreach (var dependentProperty in dependentProperties) |
| | 6 | 115 | | MetadataApplicators.ApplyAlsoValidate(builder.Metadata, dependentProperty); |
| | | 116 | | |
| | 6 | 117 | | return builder; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | |