Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FrozenDictionary #15040

Merged
merged 9 commits into from
Jan 11, 2024
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Descriptors.ShapePlacementStrategy;
Expand All @@ -10,13 +10,14 @@ public class PlacementsManager
{
private readonly IPlacementStore _placementStore;

public PlacementsManager(IPlacementStore placementStore) => _placementStore = placementStore;
public PlacementsManager(IPlacementStore placementStore)
=> _placementStore = placementStore;

public async Task<IReadOnlyDictionary<string, IEnumerable<PlacementNode>>> ListShapePlacementsAsync()
{
var document = await _placementStore.GetPlacementsAsync();

return document.Placements.ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.AsEnumerable());
return document.Placements.ToFrozenDictionary(kvp => kvp.Key, kvp => kvp.Value.AsEnumerable());
}

public async Task<IEnumerable<PlacementNode>> GetShapePlacementsAsync(string shapeType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -12,7 +12,6 @@
using Lucene.Net.Spatial.Prefix;
using Lucene.Net.Spatial.Prefix.Tree;
using Lucene.Net.Store;
using LuceneLock = Lucene.Net.Store.Lock;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Contents.Indexing;
Expand All @@ -24,6 +23,7 @@
using Spatial4n.Context;
using Directory = System.IO.Directory;
using LDirectory = Lucene.Net.Store.Directory;
using LuceneLock = Lucene.Net.Store.Lock;

namespace OrchardCore.Search.Lucene
{
Expand Down Expand Up @@ -192,7 +192,7 @@ public void Read(string indexName, Action<IndexReader> reader)
/// </summary>
public IReadOnlyDictionary<string, DateTime> GetTimestamps()
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
return new ReadOnlyDictionary<string, DateTime>(_timestamps);
return _timestamps.ToFrozenDictionary();
}

private Document CreateLuceneDocument(DocumentIndex documentIndex, LuceneIndexSettings indexSettings)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

namespace OrchardCore.AuditTrail.Services.Models
{
public class AuditTrailAdminListOptions
{
internal Dictionary<string, AuditTrailAdminListOptionBuilder> SortOptionBuilders { get; set; } = new Dictionary<string, AuditTrailAdminListOptionBuilder>();
internal Dictionary<string, AuditTrailAdminListOptionBuilder> SortOptionBuilders { get; set; } = [];

private Dictionary<string, AuditTrailAdminListOption> _sortOptions;
public IReadOnlyDictionary<string, AuditTrailAdminListOption> SortOptions => _sortOptions ??= BuildSortOptions();
private FrozenDictionary<string, AuditTrailAdminListOption> _sortOptions;

public IReadOnlyDictionary<string, AuditTrailAdminListOption> SortOptions
=> _sortOptions ??= BuildSortOptions();

private AuditTrailAdminListOption _defaultSortOption;
public AuditTrailAdminListOption DefaultSortOption => _defaultSortOption ??= SortOptions.Values.FirstOrDefault(x => x.IsDefault);
public AuditTrailAdminListOption DefaultSortOption
=> _defaultSortOption ??= SortOptions.Values.FirstOrDefault(x => x.IsDefault);

private Dictionary<string, AuditTrailAdminListOption> BuildSortOptions()
private FrozenDictionary<string, AuditTrailAdminListOption> BuildSortOptions()
{
var sortOptions = SortOptionBuilders.ToDictionary(k => k.Key, v => v.Value.Build());
var sortOptions = SortOptionBuilders.ToFrozenDictionary(k => k.Key, v => v.Value.Build());
SortOptionBuilders = null;

return sortOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Localization;

namespace OrchardCore.AuditTrail.Services.Models
{
public class AuditTrailOptions
{
internal Dictionary<string, AuditTrailCategoryDescriptorBuilder> CategoryDescriptorBuilders { get; set; } = new Dictionary<string, AuditTrailCategoryDescriptorBuilder>();
internal Dictionary<string, AuditTrailCategoryDescriptorBuilder> CategoryDescriptorBuilders { get; set; } = [];

private Dictionary<string, AuditTrailCategoryDescriptor> _categoryDescriptors;
public IReadOnlyDictionary<string, AuditTrailCategoryDescriptor> CategoryDescriptors => _categoryDescriptors ??= BuildCategoryDescriptors();
private FrozenDictionary<string, AuditTrailCategoryDescriptor> _categoryDescriptors;
public IReadOnlyDictionary<string, AuditTrailCategoryDescriptor> CategoryDescriptors
=> _categoryDescriptors ??= BuildCategoryDescriptors();

private Dictionary<string, AuditTrailCategoryDescriptor> BuildCategoryDescriptors()
private FrozenDictionary<string, AuditTrailCategoryDescriptor> BuildCategoryDescriptors()
{
var categoryDescriptors = CategoryDescriptorBuilders.ToDictionary(k => k.Key, v => v.Value.Build());
var categoryDescriptors = CategoryDescriptorBuilders.ToFrozenDictionary(k => k.Key, v => v.Value.Build());
CategoryDescriptorBuilders = null;

return categoryDescriptors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -9,14 +10,16 @@ namespace OrchardCore.ContentManagement
/// </summary>
public class ContentOptions
{
private readonly List<ContentPartOption> _contentParts = new();
private readonly List<ContentFieldOption> _contentFields = new();
private readonly List<ContentPartOption> _contentParts = [];
private readonly List<ContentFieldOption> _contentFields = [];

private IReadOnlyDictionary<string, ContentPartOption> _contentPartOptionsLookup;
public IReadOnlyDictionary<string, ContentPartOption> ContentPartOptionsLookup => _contentPartOptionsLookup ??= ContentPartOptions.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentPartOption> ContentPartOptionsLookup
=> _contentPartOptionsLookup ??= ContentPartOptions.ToFrozenDictionary(k => k.Type.Name);

private IReadOnlyDictionary<string, ContentFieldOption> _contentFieldOptionsLookup;
public IReadOnlyDictionary<string, ContentFieldOption> ContentFieldOptionsLookup => _contentFieldOptionsLookup ??= ContentFieldOptions.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentFieldOption> ContentFieldOptionsLookup
=> _contentFieldOptionsLookup ??= ContentFieldOptions.ToFrozenDictionary(k => k.Type.Name);

public IReadOnlyList<ContentPartOption> ContentPartOptions => _contentParts;
public IReadOnlyList<ContentFieldOption> ContentFieldOptions => _contentFields;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

namespace OrchardCore.ContentManagement.Display.ContentDisplay
{
public class ContentDisplayOptions
{
private readonly List<ContentPartDisplayOption> _contentParts = new();
private readonly List<ContentFieldDisplayOption> _contentFields = new();
private readonly List<ContentPartDisplayOption> _contentParts = [];
private readonly List<ContentFieldDisplayOption> _contentFields = [];

private Dictionary<string, ContentPartDisplayOption> _contentPartOptions;
public IReadOnlyDictionary<string, ContentPartDisplayOption> ContentPartOptions => _contentPartOptions ??= _contentParts.ToDictionary(k => k.Type.Name);
private FrozenDictionary<string, ContentPartDisplayOption> _contentPartOptions;
private FrozenDictionary<string, ContentFieldDisplayOption> _contentFieldOptions;

private Dictionary<string, ContentFieldDisplayOption> _contentFieldOptions;
public IReadOnlyDictionary<string, ContentFieldDisplayOption> ContentFieldOptions => _contentFieldOptions ??= _contentFields.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentPartDisplayOption> ContentPartOptions
=> _contentPartOptions ??= _contentParts.ToFrozenDictionary(k => k.Type.Name);

public IReadOnlyDictionary<string, ContentFieldDisplayOption> ContentFieldOptions
=> _contentFieldOptions ??= _contentFields.ToFrozenDictionary(k => k.Type.Name);

internal void ForContentPartDisplayMode(Type contentPartType, Type displayDriverType, Func<string, bool> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ namespace OrchardCore.ContentManagement.Display.ContentDisplay
{
public class ContentPartDisplayOption : ContentPartOptionBase
{
private readonly List<ContentPartDisplayDriverOption> _partDisplayDrivers = new();
private readonly List<ContentPartDisplayDriverOption> _partDisplayDrivers = [];

public ContentPartDisplayOption(Type contentPartType) : base(contentPartType)
{
}

private List<ContentPartDisplayDriverOption> _displayDrivers;
public IReadOnlyList<ContentPartDisplayDriverOption> DisplayDrivers => _displayDrivers ??= _partDisplayDrivers.Where(d => d.DisplayMode != null).ToList();
private IReadOnlyList<ContentPartDisplayDriverOption> _displayDrivers;
private IReadOnlyList<ContentPartDisplayDriverOption> _editorDrivers;

private List<ContentPartDisplayDriverOption> _editorDrivers;
public IReadOnlyList<ContentPartDisplayDriverOption> EditorDrivers => _editorDrivers ??= _partDisplayDrivers.Where(d => d.Editor != null).ToList();
public IReadOnlyList<ContentPartDisplayDriverOption> DisplayDrivers
=> _displayDrivers ??= _partDisplayDrivers.Where(d => d.DisplayMode != null).ToList();

public IReadOnlyList<ContentPartDisplayDriverOption> EditorDrivers
=> _editorDrivers ??= _partDisplayDrivers.Where(d => d.Editor != null).ToList();

internal void ForDisplayMode(Type displayDriverType, Func<string, bool> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Rules
{
public class ConditionOperatorOptions
{
private FrozenDictionary<string, IConditionOperatorFactory> _factories;
private FrozenDictionary<Type, ConditionOperatorOption> _conditionOperatorOptionByType;

private Dictionary<string, IConditionOperatorFactory> _factories;
public IReadOnlyDictionary<string, IConditionOperatorFactory> Factories => _factories ??= Operators.ToDictionary(x => x.Factory.Name, x => x.Factory);
public IReadOnlyDictionary<string, IConditionOperatorFactory> Factories
=> _factories ??= Operators.ToFrozenDictionary(x => x.Factory.Name, x => x.Factory);

private Dictionary<Type, ConditionOperatorOption> _conditionOperatorOptionByType;
public IReadOnlyDictionary<Type, ConditionOperatorOption> ConditionOperatorOptionByType => _conditionOperatorOptionByType ??= Operators.ToDictionary(x => x.Operator, x => x);
public IReadOnlyDictionary<Type, ConditionOperatorOption> ConditionOperatorOptionByType
=> _conditionOperatorOptionByType ??= Operators.ToFrozenDictionary(x => x.Operator, x => x);

public List<ConditionOperatorOption> Operators { get; set; } = new();
public List<ConditionOperatorOption> Operators { get; set; } = [];
}

public class ConditionOperatorOption<TLocalizer> : ConditionOperatorOption where TLocalizer : class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace OrchardCore.Sms;

public class SmsProviderOptions
{
private Dictionary<string, Type> _providers { get; } = new();
private Dictionary<string, Type> _providers { get; } = [];

private IReadOnlyDictionary<string, Type> _readonlyProviders;
private FrozenDictionary<string, Type> _readonlyProviders;

/// <summary>
/// This read-only collections contains all registered SMS providers.
/// The 'Key' is the technical name of the provider.
/// The 'Value' is the type of the SMS provider. The type will awalys be an implementation of <see cref="ISmsProvider"></see> interface.
/// The 'Value' is the type of the SMS provider. The type will always be an implementation of <see cref="ISmsProvider"></see> interface.
/// </summary>
public IReadOnlyDictionary<string, Type> Providers => _readonlyProviders ??= _providers.ToImmutableDictionary(x => x.Key, x => x.Value);
public IReadOnlyDictionary<string, Type> Providers
=> _readonlyProviders ??= _providers.ToFrozenDictionary(x => x.Key, x => x.Value);

/// <summary>
/// Adds a provider if one does not exist.
Expand Down