Was.OrcSearch – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Reflection;
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)
                {
                    var searchablePropertyAttribute =
                        property.GetCustomAttribute(typeof(SearchablePropertyAttribute), false) as
                            SearchablePropertyAttribute;
                    if (searchablePropertyAttribute != null)
                    {
                        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.