< Summary

Information
Class: MyNet.Globalization.Culture.CultureService
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Culture/CultureService.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
SetCulture(...)100%44100%
SetCulture(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Culture/CultureService.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="CultureService.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using System.Threading;
 10
 11namespace MyNet.Globalization.Culture;
 12
 13/// <summary>
 14/// Provides services for managing the current culture of the application.
 15/// Implements both <see cref="ICultureService"/> (read + write) and <see cref="ICultureContext"/> (read-only).
 16/// When the culture changes, <see cref="CultureChanged"/> is raised outside the internal lock to prevent
 17/// deadlocks in subscribers, and <see cref="CultureInfo.CurrentCulture" /> /
 18/// <see cref="CultureInfo.CurrentUICulture" /> of the calling thread are updated automatically.
 19/// </summary>
 20public sealed class CultureService(CultureInfo initialCulture) : ICultureService
 21{
 7822    private readonly Lock _lockObject = new();
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="CultureService"/> class with the current culture of the application
 26    /// </summary>
 27    public CultureService()
 1828        : this(CultureInfo.CurrentCulture) { }
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="CultureService"/> class with the default culture specified in the p
 32    /// </summary>
 33    public CultureService(GlobalizationOptions options)
 1234        : this(options.DefaultCulture) { }
 35
 36    /// <inheritdoc />
 37    public CultureInfo CurrentCulture { get; private set; } = initialCulture;
 38
 39    /// <inheritdoc />
 40    public event EventHandler<CultureChangedEventArgs>? CultureChanged;
 41
 42    /// <inheritdoc />
 43    /// <remarks>
 44    /// This method is thread-safe. The <see cref="CultureChanged"/> event is raised <b>outside</b> the internal lock.
 45    /// <see cref="CultureInfo.CurrentCulture"/> and <see cref="CultureInfo.CurrentUICulture"/> of the calling
 46    /// thread are updated so that standard .NET formatting APIs reflect the new culture immediately.
 47    /// </remarks>
 48    public void SetCulture(CultureInfo culture)
 49    {
 581750        ArgumentNullException.ThrowIfNull(culture);
 51
 52        CultureChangedEventArgs? args;
 53        lock (_lockObject)
 54        {
 685855            if (Equals(CurrentCulture, culture)) return;
 56
 477057            var old = CurrentCulture;
 477058            CurrentCulture = culture;
 477059            args = new(old, culture);
 477060        }
 61
 62        // Update the calling thread's culture so that formatting APIs (e.g. ResourceManager, string.Format)
 63        // reflect the change without requiring callers to pass the culture explicitly.
 477064        CultureInfo.CurrentCulture = culture;
 477065        CultureInfo.CurrentUICulture = culture;
 66
 67        // Raise event outside the lock to prevent deadlocks when subscribers call back into this service.
 477068        CultureChanged?.Invoke(this, args);
 580269    }
 70
 71    /// <inheritdoc />
 5472    public void SetCulture(string cultureCode) => SetCulture(CultureInfo.GetCultureInfo(cultureCode));
 73}
 74