| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GlobalizationServiceProxy.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using MyNet.Globalization.Culture; |
| | | 10 | | using MyNet.Globalization.DateTime; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Globalization.Facade; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stable <see cref="IGlobalizationService"/> facade whose target can be swapped at startup. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class GlobalizationServiceProxy : IGlobalizationService |
| | | 18 | | { |
| | | 19 | | private IGlobalizationService _inner; |
| | | 20 | | |
| | | 21 | | private event EventHandler<CultureChangedEventArgs>? CultureChangedInternal; |
| | | 22 | | |
| | | 23 | | private event EventHandler<TimeZoneChangedEventArgs>? TimeZoneChangedInternal; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="GlobalizationServiceProxy"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="inner">Initial globalization service implementation.</param> |
| | | 29 | | public GlobalizationServiceProxy(IGlobalizationService inner) |
| | | 30 | | { |
| | 9 | 31 | | _inner = inner ?? throw new ArgumentNullException(nameof(inner)); |
| | 9 | 32 | | WireInnerEvents(); |
| | 9 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | 153 | 36 | | public CultureInfo CurrentCulture => _inner.CurrentCulture; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 0 | 39 | | public TimeZoneInfo CurrentTimeZone => _inner.CurrentTimeZone; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 0 | 42 | | public System.DateTime Now => _inner.Now; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public event EventHandler<CultureChangedEventArgs> CultureChanged |
| | | 46 | | { |
| | 162 | 47 | | add => CultureChangedInternal += value; |
| | 3 | 48 | | remove => CultureChangedInternal -= value; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public event EventHandler<TimeZoneChangedEventArgs> TimeZoneChanged |
| | | 53 | | { |
| | 3 | 54 | | add => TimeZoneChangedInternal += value; |
| | 3 | 55 | | remove => TimeZoneChangedInternal -= value; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Replaces the forwarded globalization service implementation. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="target">The configured globalization service.</param> |
| | | 62 | | public void SetTarget(IGlobalizationService target) |
| | | 63 | | { |
| | 6 | 64 | | ArgumentNullException.ThrowIfNull(target); |
| | | 65 | | |
| | 6 | 66 | | if (ReferenceEquals(_inner, target)) |
| | 0 | 67 | | return; |
| | | 68 | | |
| | 6 | 69 | | UnwireInnerEvents(); |
| | 6 | 70 | | _inner = target; |
| | 6 | 71 | | WireInnerEvents(); |
| | 6 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | 5703 | 75 | | public void SetCulture(CultureInfo culture) => _inner.SetCulture(culture); |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | 51 | 78 | | public void SetCulture(string cultureCode) => _inner.SetCulture(cultureCode); |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | 0 | 81 | | public void SetTimeZone(TimeZoneInfo timeZone) => _inner.SetTimeZone(timeZone); |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | 0 | 84 | | public void SetTimeZone(string timeZoneId) => _inner.SetTimeZone(timeZoneId); |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | 0 | 87 | | public DateTimeOffset FromUtc(DateTimeOffset utcDateTime) => _inner.FromUtc(utcDateTime); |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | 0 | 90 | | public DateTimeOffset ToUtc(DateTimeOffset localDateTime) => _inner.ToUtc(localDateTime); |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public DateTimeOffset Convert(DateTimeOffset dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone |
| | 0 | 94 | | => _inner.Convert(dateTime, sourceTimeZone, destinationTimeZone); |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | 0 | 97 | | public IDisposable UseCulture(CultureInfo culture) => _inner.UseCulture(culture); |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | 0 | 100 | | public IDisposable UseTimeZone(TimeZoneInfo timeZone) => _inner.UseTimeZone(timeZone); |
| | | 101 | | |
| | | 102 | | private void WireInnerEvents() |
| | | 103 | | { |
| | 15 | 104 | | _inner.CultureChanged += ForwardCultureChanged; |
| | 15 | 105 | | _inner.TimeZoneChanged += ForwardTimeZoneChanged; |
| | 15 | 106 | | } |
| | | 107 | | |
| | | 108 | | private void UnwireInnerEvents() |
| | | 109 | | { |
| | 6 | 110 | | _inner.CultureChanged -= ForwardCultureChanged; |
| | 6 | 111 | | _inner.TimeZoneChanged -= ForwardTimeZoneChanged; |
| | 6 | 112 | | } |
| | | 113 | | |
| | 4749 | 114 | | private void ForwardCultureChanged(object? sender, CultureChangedEventArgs e) => CultureChangedInternal?.Invoke(this |
| | | 115 | | |
| | 0 | 116 | | private void ForwardTimeZoneChanged(object? sender, TimeZoneChangedEventArgs e) => TimeZoneChangedInternal?.Invoke(t |
| | | 117 | | } |
| | | 118 | | |