| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BehaviorExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.ComponentModel; |
| | | 9 | | using System.Linq.Expressions; |
| | | 10 | | using FluentValidation; |
| | | 11 | | using MyNet.Globalization.Culture; |
| | | 12 | | using MyNet.Globalization.DateTime; |
| | | 13 | | using MyNet.Observable.Behaviors; |
| | | 14 | | using MyNet.Primitives; |
| | | 15 | | |
| | | 16 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 17 | | namespace MyNet.Observable; |
| | | 18 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Provides extension methods for the <see cref="ObservableObject"/> class, allowing to easily add behaviors to an obse |
| | | 22 | | /// </summary> |
| | | 23 | | public static class BehaviorExtensions |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Adds localization behavior to the specified observable object. This method registers a new instance of <see cref |
| | | 27 | | /// </summary> |
| | | 28 | | extension<TOwner>(TOwner owner) |
| | | 29 | | where TOwner : ObservableObject |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Adds localization behavior to the specified observable object. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="cultureService">The culture service used to handle culture changes.</param> |
| | | 35 | | /// <returns>The observable object with localization behavior added.</returns> |
| | | 36 | | public ObservableObject ReactOnCultureChanged(ICultureService cultureService) |
| | | 37 | | { |
| | 168 | 38 | | owner.Behaviors.Register(new CultureChangedBehavior(owner, cultureService)); |
| | | 39 | | |
| | 168 | 40 | | return owner; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Adds time zone localization behavior to the specified observable object. This method registers a new instanc |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="timeZoneService">The time zone service used to handle time zone changes.</param> |
| | | 47 | | /// <returns>The observable object with time zone localization behavior added.</returns> |
| | | 48 | | public ObservableObject ReactOnTimeZoneChanged(ITimeZoneService timeZoneService) |
| | | 49 | | { |
| | 3 | 50 | | owner.Behaviors.Register(new TimeZoneChangedBehavior(owner, timeZoneService)); |
| | | 51 | | |
| | 3 | 52 | | return owner; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Adds selection state (<see cref="ISelectable"/>) to the observable object. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="isSelectable">Initial value for <see cref="ISelectable.IsSelectable"/>.</param> |
| | | 59 | | /// <returns>The registered <see cref="SelectionBehavior"/>.</returns> |
| | | 60 | | public SelectionBehavior UseSelection(bool isSelectable = true) |
| | | 61 | | { |
| | 0 | 62 | | var behavior = new SelectionBehavior(owner, isSelectable); |
| | 0 | 63 | | owner.Behaviors.Register(behavior); |
| | 0 | 64 | | return behavior; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Adds modification tracking behavior to the specified observable object. This method registers a new instance |
| | | 69 | | /// </summary> |
| | | 70 | | /// <returns>The observable object with modification tracking behavior added.</returns> |
| | | 71 | | public ObservableObject UseTracking() |
| | | 72 | | { |
| | 48 | 73 | | owner.Behaviors.Register(new ModificationTrackingBehavior(owner)); |
| | | 74 | | |
| | 48 | 75 | | return owner; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Adds validation behavior to the specified observable object. This method registers a new instance of <see cr |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="validator">The validator used to validate the observable object's properties.</param> |
| | | 82 | | /// <returns>The validation behavior added to the observable object.</returns> |
| | | 83 | | public ValidationBehavior<TOwner> UseValidation(IValidator validator) |
| | | 84 | | { |
| | 81 | 85 | | var behavior = new ValidationBehavior<TOwner>(owner, validator); |
| | | 86 | | |
| | 81 | 87 | | owner.Behaviors.Register(behavior); |
| | | 88 | | |
| | 81 | 89 | | return behavior; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Registers a <see cref="PropertyChangedForwardingBehavior"/> for the property selected by the expression. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <typeparam name="T">The type of the property.</typeparam> |
| | | 96 | | /// <param name="selector">Expression selecting the property (e.g. x => x.Wrapper).</param> |
| | | 97 | | /// <param name="concatenatePropertyName">True to emit Wrapper.Name; false to emit Name only.</param> |
| | | 98 | | /// <returns>The observable object.</returns> |
| | | 99 | | public ObservableObject ForwardProperty<T>(Expression<Func<TOwner, T?>> selector, bool concatenatePropertyName = |
| | | 100 | | where T : class, INotifyPropertyChanged |
| | | 101 | | { |
| | 18 | 102 | | MetadataBehaviorApplicator.ApplyForwardProperty(owner, selector.PropertyName(), concatenatePropertyName); |
| | 18 | 103 | | return owner; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Registers a <see cref="PropertyChangedForwardingBehavior"/> for the provided property name. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="propertyName">The wrapper property name.</param> |
| | | 110 | | /// <param name="concatenatePropertyName">True to emit Wrapper.Name; false to emit Name only.</param> |
| | | 111 | | /// <returns>The observable object.</returns> |
| | | 112 | | public ObservableObject ForwardProperty(string propertyName, bool concatenatePropertyName = true) |
| | | 113 | | { |
| | 0 | 114 | | MetadataBehaviorApplicator.ApplyForwardProperty(owner, propertyName, concatenatePropertyName); |
| | 0 | 115 | | return owner; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |