Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NameCompatibilityManager.cs

using Nop.Core.ComponentModel;
using Nop.Core.Infrastructure;

namespace Nop.Data.Mapping;

/// 
/// Helper class for maintaining backward compatibility of table naming
/// 
public static partial class NameCompatibilityManager
{
    #region Fields

    private static readonly Dictionary _tableNames = new();
    private static readonly Dictionary<(Type, string), string> _columnName = new();
    private static readonly IList _loadedFor = new List();
    private static bool _isInitialized;
    private static readonly ReaderWriterLockSlim _locker = new();

    #endregion

    #region Utilities

    private static void Initialize()
    {
        //perform with locked access to resources
        using (new ReaderWriteLockDisposable(_locker))
        {
            if (_isInitialized)
                return;

            var typeFinder = Singleton.Instance;
            var compatibilities = typeFinder.FindClassesOfType()
                ?.Select(type => EngineContext.Current.ResolveUnregistered(type) as INameCompatibility).ToList() ?? new List();

            compatibilities.AddRange(AdditionalNameCompatibilities.Select(type => EngineContext.Current.ResolveUnregistered(type) as INameCompatibility));

            foreach (var nameCompatibility in compatibilities.Distinct())
            {
                if (_loadedFor.Contains(nameCompatibility.GetType()))
                    continue;

                _loadedFor.Add(nameCompatibility.GetType());

                foreach (var (key, value) in nameCompatibility.TableNames.Where(tableName =>
                             !_tableNames.ContainsKey(tableName.Key)))
                    _tableNames.Add(key, value);

                foreach (var (key, value) in nameCompatibility.ColumnName.Where(columnName =>
                             !_columnName.ContainsKey(columnName.Key)))
                    _columnName.Add(key, value);
            }

            _isInitialized = true;
        }
    }

    #endregion

    #region Methods

    /// 
    /// Gets table name for mapping with the type
    /// 
    /// Type to get the table name
    /// Table name
    public static string GetTableName(Type type)
    {
        if (!_isInitialized)
            Initialize();

        return _tableNames.TryGetValue(type, out var value) ? value : type.Name;
    }

    /// 
    /// Gets column name for mapping with the entity's property
    /// 
    /// Type of entity
    /// Property name
    /// Column name
    public static string GetColumnName(Type type, string propertyName)
    {
        if (!_isInitialized)
            Initialize();

        return _columnName.ContainsKey((type, propertyName)) ? _columnName[(type, propertyName)] : propertyName;
    }

    #endregion

    /// 
    /// Additional name compatibility types
    /// 
    public static List AdditionalNameCompatibilities { get; } = new List();
}