|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq.Expressions; |
4 | | -using Elasticsearch.Net; |
5 | 4 |
|
6 | 5 | namespace Nest |
7 | 6 | { |
8 | | - public interface IClrTypeMapping<T> where T : class |
| 7 | + public interface IClrTypeMapping |
9 | 8 | { |
10 | | - Type Type { get; } |
| 9 | + /// <summary> |
| 10 | + /// The CLR type the mapping relates to |
| 11 | + /// </summary> |
| 12 | + Type ClrType { get; } |
11 | 13 |
|
12 | 14 | /// <summary> |
13 | | - /// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/> |
| 15 | + /// The default Elasticsearch index name for <see cref="ClrType"/> |
14 | 16 | /// </summary> |
15 | 17 | string IndexName { get; set; } |
16 | 18 |
|
17 | 19 | /// <summary> |
18 | | - /// When specified dictates the default Elasticsearch type name for <typeparamref name="T" /> |
| 20 | + /// The default Elasticsearch type name for <see cref="ClrType"/> |
19 | 21 | /// </summary> |
20 | 22 | string TypeName { get; set; } |
21 | 23 |
|
22 | 24 | /// <summary> |
23 | | - /// When specified dictates the relation name for <typeparamref name="T" /> to resolve to. |
| 25 | + /// The relation name for <see cref="ClrType"/> to resolve to. |
24 | 26 | /// </summary> |
25 | 27 | string RelationName { get; set; } |
| 28 | + } |
26 | 29 |
|
| 30 | + public interface IClrTypeMapping<TDocument> : IClrTypeMapping where TDocument : class |
| 31 | + { |
27 | 32 | /// <summary> |
28 | | - /// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate |
| 33 | + /// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate |
29 | 34 | /// </summary> |
30 | | - Expression<Func<T, object>> IdProperty { get; set; } |
| 35 | + Expression<Func<TDocument, object>> IdProperty { get; set; } |
31 | 36 |
|
32 | 37 | /// <summary> |
33 | | - /// When specified allows you to ignore or rename certain properties of clr type <typeparamref name="T" /> |
| 38 | + /// Ignore or rename certain properties of CLR type <typeparamref name="TDocument" /> |
34 | 39 | /// </summary> |
35 | | - IList<IClrTypePropertyMapping<T>> Properties { get; set; } |
| 40 | + IList<IClrPropertyMapping<TDocument>> Properties { get; set; } |
36 | 41 | } |
37 | 42 |
|
38 | | - public class ClrTypeMapping<T> : IClrTypeMapping<T> where T : class |
| 43 | + public class ClrTypeMapping : IClrTypeMapping |
39 | 44 | { |
40 | | - public Type Type { get; } = typeof (T); |
41 | | - |
42 | 45 | /// <summary> |
43 | | - /// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/> |
| 46 | + /// Initializes a new instance of <see cref="ClrTypeMapping"/> |
44 | 47 | /// </summary> |
| 48 | + public ClrTypeMapping(Type type) => ClrType = type; |
| 49 | + |
| 50 | + /// <inheritdoc /> |
| 51 | + public Type ClrType { get; } |
| 52 | + |
| 53 | + /// <inheritdoc /> |
45 | 54 | public string IndexName { get; set; } |
46 | 55 |
|
47 | | - /// <summary> |
48 | | - /// When specified dictates the default Elasticsearch type name for <typeparamref name="T" /> |
49 | | - /// </summary> |
| 56 | + /// <inheritdoc /> |
50 | 57 | public string TypeName { get; set; } |
51 | 58 |
|
52 | | - /// <summary> |
53 | | - /// When specified dictates the relation name for <typeparamref name="T" /> to resolve to. |
54 | | - /// </summary> |
| 59 | + /// <inheritdoc /> |
55 | 60 | public string RelationName { get; set; } |
56 | 61 |
|
57 | | - /// <summary> |
58 | | - /// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate |
59 | | - /// </summary> |
60 | | - public Expression<Func<T, object>> IdProperty { get; set; } |
| 62 | + } |
| 63 | + public class ClrTypeMapping<TDocument> : ClrTypeMapping, IClrTypeMapping<TDocument> where TDocument : class |
| 64 | + { |
| 65 | + public ClrTypeMapping() : base(typeof(TDocument)) { } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public Expression<Func<TDocument, object>> IdProperty { get; set; } |
61 | 69 |
|
62 | | - public IList<IClrTypePropertyMapping<T>> Properties { get; set; } |
| 70 | + /// <inheritdoc /> |
| 71 | + public IList<IClrPropertyMapping<TDocument>> Properties { get; set; } |
63 | 72 | } |
64 | 73 |
|
65 | | - public class ClrTypeMappingDescriptor<T> : DescriptorBase<ClrTypeMappingDescriptor<T>,IClrTypeMapping<T>> , IClrTypeMapping<T> |
66 | | - where T : class |
| 74 | + public class ClrTypeMappingDescriptor : DescriptorBase<ClrTypeMappingDescriptor,IClrTypeMapping> , IClrTypeMapping |
67 | 75 | { |
68 | | - Type IClrTypeMapping<T>.Type { get; } = typeof (T); |
69 | | - string IClrTypeMapping<T>.IndexName { get; set; } |
70 | | - string IClrTypeMapping<T>.TypeName { get; set; } |
71 | | - string IClrTypeMapping<T>.RelationName { get; set; } |
72 | | - Expression<Func<T, object>> IClrTypeMapping<T>.IdProperty { get; set; } |
73 | | - IList<IClrTypePropertyMapping<T>> IClrTypeMapping<T>.Properties { get; set; } = new List<IClrTypePropertyMapping<T>>(); |
| 76 | + private readonly Type _type; |
74 | 77 |
|
75 | 78 | /// <summary> |
76 | | - /// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/> |
| 79 | + /// Instantiates a new instance of <see cref="ClrTypeMappingDescriptor"/> |
77 | 80 | /// </summary> |
78 | | - public ClrTypeMappingDescriptor<T> IndexName(string indexName) => Assign(a => a.IndexName = indexName); |
| 81 | + /// <param name="type">The CLR type to map</param> |
| 82 | + public ClrTypeMappingDescriptor(Type type) => _type = type; |
79 | 83 |
|
80 | | - /// <summary> |
81 | | - /// When specified dictates the default Elasticsearch type name for <typeparamref name="T" /> |
82 | | - /// </summary> |
83 | | - public ClrTypeMappingDescriptor<T> TypeName(string typeName) => Assign(a => a.TypeName = typeName); |
| 84 | + Type IClrTypeMapping.ClrType => _type; |
| 85 | + string IClrTypeMapping.IndexName { get; set; } |
| 86 | + string IClrTypeMapping.TypeName { get; set; } |
| 87 | + string IClrTypeMapping.RelationName { get; set; } |
84 | 88 |
|
85 | 89 | /// <summary> |
86 | | - /// When specified dictates the relation name for <typeparamref name="T" /> to resolve to. |
| 90 | + /// The default Elasticsearch index name for the CLR type |
87 | 91 | /// </summary> |
88 | | - public ClrTypeMappingDescriptor<T> RelationName(string relationName) => Assign(a => a.RelationName = relationName); |
| 92 | + public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName); |
89 | 93 |
|
90 | 94 | /// <summary> |
91 | | - /// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate |
| 95 | + /// The default Elasticsearch type name for the CLR type |
92 | 96 | /// </summary> |
93 | | - public ClrTypeMappingDescriptor<T> IdProperty(Expression<Func<T, object>> property) => Assign(a => a.IdProperty = property); |
| 97 | + public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName); |
94 | 98 |
|
95 | 99 | /// <summary> |
96 | | - /// When specified allows you to ignore <param name="property"></param> on clr type <typeparamref name="T" /> |
| 100 | + /// The relation name for the CLR type to resolve to. |
97 | 101 | /// </summary> |
98 | | - public ClrTypeMappingDescriptor<T> Ignore(Expression<Func<T, object>> property) => |
99 | | - Assign(a => a.Properties.Add(new IgnorePropertyMapping<T>(property))); |
100 | | - |
101 | | - /// <summary> |
102 | | - /// When specified allows you to rename <param name="property"></param> on clr type <typeparamref name="T" /> |
103 | | - /// </summary> |
104 | | - public ClrTypeMappingDescriptor<T> Rename(Expression<Func<T, object>> property, string newName) => |
105 | | - Assign(a => a.Properties.Add(new RenamePropertyMapping<T>(property, newName))); |
106 | | - |
| 102 | + public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName); |
107 | 103 | } |
108 | 104 |
|
109 | | - public interface IClrTypePropertyMapping<T> where T : class |
| 105 | + public class ClrTypeMappingDescriptor<TDocument> |
| 106 | + : DescriptorBase<ClrTypeMappingDescriptor<TDocument>,IClrTypeMapping<TDocument>>, IClrTypeMapping<TDocument> |
| 107 | + where TDocument : class |
110 | 108 | { |
111 | | - Expression<Func<T, object>> Property { get; set; } |
112 | | - bool Ignore { get; set; } |
113 | | - string NewName { get; set; } |
114 | | - IPropertyMapping ToPropertyMapping(); |
115 | | - } |
116 | | - |
117 | | - public abstract class ClrPropertyMappingBase<T> : IClrTypePropertyMapping<T> |
118 | | - where T : class |
119 | | - { |
120 | | - protected IClrTypePropertyMapping<T> Self => this; |
| 109 | + Type IClrTypeMapping.ClrType { get; } = typeof (TDocument); |
| 110 | + string IClrTypeMapping.IndexName { get; set; } |
| 111 | + string IClrTypeMapping.TypeName { get; set; } |
| 112 | + string IClrTypeMapping.RelationName { get; set; } |
| 113 | + Expression<Func<TDocument, object>> IClrTypeMapping<TDocument>.IdProperty { get; set; } |
| 114 | + IList<IClrPropertyMapping<TDocument>> IClrTypeMapping<TDocument>.Properties { get; set; } = new List<IClrPropertyMapping<TDocument>>(); |
121 | 115 |
|
122 | | - Expression<Func<T, object>> IClrTypePropertyMapping<T>.Property { get; set; } |
| 116 | + /// <summary> |
| 117 | + /// The default Elasticsearch index name for <typeparamref name="TDocument"/> |
| 118 | + /// </summary> |
| 119 | + public ClrTypeMappingDescriptor<TDocument> IndexName(string indexName) => Assign(a => a.IndexName = indexName); |
123 | 120 |
|
124 | | - bool IClrTypePropertyMapping<T>.Ignore { get; set; } |
| 121 | + /// <summary> |
| 122 | + /// The default Elasticsearch type name for <typeparamref name="TDocument" /> |
| 123 | + /// </summary> |
| 124 | + public ClrTypeMappingDescriptor<TDocument> TypeName(string typeName) => Assign(a => a.TypeName = typeName); |
125 | 125 |
|
126 | | - string IClrTypePropertyMapping<T>.NewName { get; set; } |
| 126 | + /// <summary> |
| 127 | + /// The relation name for <typeparamref name="TDocument" /> to resolve to. |
| 128 | + /// </summary> |
| 129 | + public ClrTypeMappingDescriptor<TDocument> RelationName(string relationName) => Assign(a => a.RelationName = relationName); |
127 | 130 |
|
128 | | - protected ClrPropertyMappingBase(Expression<Func<T, object>> property) |
129 | | - { |
130 | | - Self.Property = property; |
131 | | - } |
| 131 | + /// <summary> |
| 132 | + /// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate |
| 133 | + /// </summary> |
| 134 | + public ClrTypeMappingDescriptor<TDocument> IdProperty(Expression<Func<TDocument, object>> property) => Assign(a => a.IdProperty = property); |
132 | 135 |
|
133 | | - IPropertyMapping IClrTypePropertyMapping<T>.ToPropertyMapping() => Self.Ignore ? PropertyMapping.Ignored : new PropertyMapping {Name = Self.NewName}; |
134 | | - } |
| 136 | + /// <summary> |
| 137 | + /// Ignore <paramref name="property" /> on CLR type <typeparamref name="TDocument" /> |
| 138 | + /// </summary> |
| 139 | + public ClrTypeMappingDescriptor<TDocument> Ignore(Expression<Func<TDocument, object>> property) => |
| 140 | + Assign(a => a.Properties.Add(new IgnoreClrPropertyMapping<TDocument>(property))); |
135 | 141 |
|
136 | | - public class IgnorePropertyMapping<T> : ClrPropertyMappingBase<T> where T : class |
137 | | - { |
138 | | - public IgnorePropertyMapping(Expression<Func<T, object>> property) : base(property) |
139 | | - { |
140 | | - Self.Ignore = true; |
141 | | - } |
142 | | - } |
| 142 | + /// <summary> |
| 143 | + /// Rename <paramref name="property" /> on CLR type <typeparamref name="TDocument" /> |
| 144 | + /// </summary> |
| 145 | + public ClrTypeMappingDescriptor<TDocument> Rename(Expression<Func<TDocument, object>> property, string newName) => |
| 146 | + Assign(a => a.Properties.Add(new RenameClrPropertyMapping<TDocument>(property, newName))); |
143 | 147 |
|
144 | | - public class RenamePropertyMapping<T> : ClrPropertyMappingBase<T> where T : class |
145 | | - { |
146 | | - public RenamePropertyMapping(Expression<Func<T, object>> property, string newName) : base(property) |
147 | | - { |
148 | | - newName.ThrowIfNull(nameof(newName)); |
149 | | - Self.NewName = newName; |
150 | | - } |
151 | 148 | } |
152 | | - |
153 | | - |
154 | | - |
155 | 149 | } |
0 commit comments