Was.OrcSearch – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.Reflection; |
||
4 | using Was.OrcSearch.Attributes; |
||
5 | using Was.OrcSearch.Models; |
||
6 | using Was.OrcSearch.Models.Interfaces; |
||
7 | |||
8 | namespace Was.OrcSearch.Metadata |
||
9 | { |
||
10 | public class AttributeMetadataCollection : ReflectionMetadataCollection |
||
11 | { |
||
12 | private static readonly Dictionary<Type, List<SearchableMetadata>> _propertiesCache = |
||
13 | new Dictionary<Type, List<SearchableMetadata>>(); |
||
14 | |||
15 | private readonly Type _targetType; |
||
16 | |||
17 | public AttributeMetadataCollection(Type targetType) |
||
18 | : base(targetType) |
||
19 | { |
||
20 | _targetType = targetType; |
||
21 | } |
||
22 | |||
23 | public override IEnumerable<IMetadata> All |
||
24 | { |
||
25 | get |
||
26 | { |
||
27 | if (_propertiesCache.TryGetValue(_targetType, out var storedProperty)) |
||
28 | return storedProperty; |
||
29 | |||
30 | var searchableProperties = new List<SearchableMetadata>(); |
||
31 | |||
32 | var properties = _targetType.GetProperties(); |
||
33 | foreach (var property in properties) |
||
34 | { |
||
35 | var searchablePropertyAttribute = |
||
36 | property.GetCustomAttribute(typeof(SearchablePropertyAttribute), false) as |
||
37 | SearchablePropertyAttribute; |
||
38 | if (searchablePropertyAttribute != null) |
||
39 | { |
||
40 | var searchableProperty = new SearchableMetadata(property); |
||
41 | if (!string.IsNullOrWhiteSpace(searchablePropertyAttribute.SearchName)) |
||
42 | searchableProperty.SearchName = searchablePropertyAttribute.SearchName; |
||
43 | |||
44 | searchableProperty.Analyze = searchablePropertyAttribute.Analyze; |
||
45 | |||
46 | searchableProperties.Add(searchableProperty); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | _propertiesCache.Add(_targetType, searchableProperties); |
||
51 | return searchableProperties; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | } |