using System; using System.Collections.Concurrent; using System.Reflection; namespace InfluxDB.Client.Core.Flux.Internal { /// /// The cache for DomainObject attributes. The attributes are used for mapping from/to DomainObject. /// public class AttributesCache { // Reflection results are cached for poco type property and attribute lookups as an optimization since // calls are invoked continuously for a given type and will not change over library lifetime private static readonly ConcurrentDictionary PropertyCache = new ConcurrentDictionary(); private static readonly ConcurrentDictionary AttributeCache = new ConcurrentDictionary(); /// /// Get properties for specified Type. /// /// type of DomainObject /// properties for DomainObject public PropertyInfo[] GetProperties(Type type) { Arguments.CheckNotNull(type, nameof(type)); return PropertyCache.GetOrAdd(type, _ => type.GetProperties()); } /// /// Get Mapping attribute for specified property. /// /// property of DomainObject /// Property Attribute public Column GetAttribute(PropertyInfo property) { Arguments.CheckNotNull(property, nameof(property)); return AttributeCache.GetOrAdd(property, _ => { var attributes = property.GetCustomAttributes(typeof(Column), false); return attributes.Length > 0 ? attributes[0] as Column : null; }); } /// /// Get name of field or tag for specified attribute and property /// /// attribute of DomainObject /// property of DomainObject /// name used for mapping public string GetColumnName(Column attribute, PropertyInfo property) { Arguments.CheckNotNull(property, nameof(property)); if (attribute != null && !string.IsNullOrEmpty(attribute.Name)) { return attribute.Name; } return property.Name; } } }