Was.OrcSearch – Rev 4

Subversion Repositories:
Rev:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using Was.OrcSearch.Attributes;
using Was.OrcSearch.Models;
using Was.OrcSearch.Models.Interfaces;

namespace Was.OrcSearch.Metadata
{
    public class AttributeMetadataCollection : ReflectionMetadataCollection
    {
        private static readonly Dictionary<Type, List<SearchableMetadata>> PropertiesCache =
            new Dictionary<Type, List<SearchableMetadata>>();

        private readonly Type _targetType;

        public AttributeMetadataCollection(Type targetType)
            : base(targetType)
        {
            _targetType = targetType;
        }

        public override IEnumerable<IMetadata> All
        {
            get
            {
                if (PropertiesCache.TryGetValue(_targetType, out var storedProperty))
                    return storedProperty;

                var searchableProperties = new List<SearchableMetadata>();
                var properties = _targetType.GetProperties();
                foreach (var property in properties)
                {
                    if (!(property.GetCustomAttribute(typeof(SearchablePropertyAttribute), false) is
                        SearchablePropertyAttribute searchablePropertyAttribute)) continue;
                    
                    var searchableProperty = new SearchableMetadata(property);
                    if (!string.IsNullOrWhiteSpace(searchablePropertyAttribute.SearchName))
                        searchableProperty.SearchName = searchablePropertyAttribute.SearchName;

                    searchableProperty.Analyze = searchablePropertyAttribute.Analyze;

                    searchableProperties.Add(searchableProperty);
                }

                PropertiesCache.Add(_targetType, searchableProperties);
                return searchableProperties;
            }
        }
    }
}

Generated by GNU Enscript 1.6.5.90.