Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
// Created: 2007.08.27

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using Xtensive.Core;
Expand Down Expand Up @@ -36,12 +38,15 @@ public sealed class NameBuilder
private const string ReferenceForeignKeyFormat = "FK_{0}_{1}_{2}";
private const string HierarchyForeignKeyFormat = "FK_{0}_{1}";

private readonly Dictionary<Pair<Type, string>, string> fieldNameCache = new Dictionary<Pair<Type, string>, string>();
private readonly object _lock = new object();
private static readonly Func<PropertyInfo, string> fieldNameCacheValueFactory =
field => field.GetAttribute<OverrideFieldNameAttribute>()?.Name ?? field.Name;

private readonly int maxIdentifierLength;
private readonly NamingConvention namingConvention;
private readonly bool isMultidatabase;
private readonly string defaultDatabase;
private readonly ConcurrentDictionary<PropertyInfo, string> fieldNameCache =
new ConcurrentDictionary<PropertyInfo, string>();

/// <summary>
/// Gets the <see cref="Entity.TypeId"/> column name.
Expand Down Expand Up @@ -181,24 +186,9 @@ public string BuildFieldName(FieldDef field)
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private string BuildFieldNameInternal(PropertyInfo propertyInfo)
{
var key = new Pair<Type, string>(propertyInfo.ReflectedType, propertyInfo.Name);

lock (fieldNameCache) {
string result;
if (fieldNameCache.TryGetValue(key, out result))
return result;
var attribute = propertyInfo.GetAttribute<OverrideFieldNameAttribute>();
if (attribute!=null) {
result = attribute.Name;
fieldNameCache.Add(key, result);
return result;
}
}

return propertyInfo.Name;
}
=> fieldNameCache.GetOrAdd(propertyInfo, fieldNameCacheValueFactory);

/// <summary>
/// Builds the name of the field.
Expand Down