comparison Source/Mapping/Fluent/FluentMap.Interface.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Data;
4 using System.Linq;
5
6 using BLToolkit.Reflection;
7 using BLToolkit.Reflection.Extension;
8
9 namespace BLToolkit.Mapping.Fluent
10 {
11 public partial class FluentMap<T> : IFluentMap
12 {
13 /// <summary>
14 /// Get mapping
15 /// </summary>
16 /// <returns></returns>
17 ExtensionList IFluentMap.Map()
18 {
19 return this.Map();
20 }
21
22 /// <summary>
23 /// TableNameAttribute
24 /// </summary>
25 /// <param name="database"></param>
26 /// <param name="owner"></param>
27 /// <param name="name"></param>
28 /// <returns></returns>
29 void IFluentMap.TableName(string database, string owner, string name)
30 {
31 if (null != database)
32 {
33 this._typeExtension.Attributes.Add(Attributes.TableName.Database, database);
34 }
35 if (null != owner)
36 {
37 this._typeExtension.Attributes.Add(Attributes.TableName.Owner, owner);
38 }
39 if (null != name)
40 {
41 this._typeExtension.Attributes.Add(Attributes.TableName.Name, name);
42 }
43 this.EachChilds(m => m.TableName(database, owner, name));
44 }
45
46 /// <summary>
47 /// Map to ExtensionList
48 /// </summary>
49 /// <param name="extensions"></param>
50 void IFluentMap.MapTo(ExtensionList extensions)
51 {
52 this.MapTo(extensions);
53 }
54
55 /// <summary>
56 /// Maps the field.
57 /// </summary>
58 /// <param name="propName">Name of the prop.</param>
59 /// <param name="mapName">Name of the map.</param>
60 /// <param name="storage">The storage.</param>
61 /// <param name="isInheritanceDiscriminator">The is inheritance discriminator.</param>
62 /// <returns></returns>
63 void IFluentMap.MapField(string propName, string mapName, string storage, bool? isInheritanceDiscriminator)
64 {
65 if (propName.Contains(MemberNameSeparator))
66 {
67 this.MapFieldOnType(propName, mapName);
68 }
69 else
70 {
71 this.MapFieldOnField(propName, mapName, storage, isInheritanceDiscriminator);
72 }
73 this.EachChilds(m => m.MapField(propName, mapName, storage, isInheritanceDiscriminator));
74 }
75
76 /// <summary>
77 /// Primaries the key.
78 /// </summary>
79 /// <param name="propName">Name of the prop.</param>
80 /// <param name="order">The order.</param>
81 void IFluentMap.PrimaryKey(string propName, int order)
82 {
83 var member = this.GetMemberExtension(propName);
84 member.Attributes.Add(Attributes.PrimaryKey.Order, Convert.ToString(order));
85 this.EachChilds(m => m.PrimaryKey(propName, order));
86 }
87
88 /// <summary>
89 /// Nons the updatable.
90 /// </summary>
91 /// <param name="propName">Name of the prop.</param>
92 void IFluentMap.NonUpdatable(string propName)
93 {
94 var member = this.GetMemberExtension(propName);
95 member.Attributes.Add(Attributes.NonUpdatable, this.ToString(true));
96 this.EachChilds(m => m.NonUpdatable(propName));
97 }
98
99 /// <summary>
100 /// Identities the specified prop name.
101 /// </summary>
102 /// <param name="propName">Name of the prop.</param>
103 void IFluentMap.Identity(string propName)
104 {
105 var member = this.GetMemberExtension(propName);
106 member.Attributes.Add(Attributes.Identity, this.ToString(true));
107 this.EachChilds(m => m.Identity(propName));
108 }
109
110 /// <summary>
111 /// SQLs the ignore.
112 /// </summary>
113 /// <param name="propName">Name of the prop.</param>
114 /// <param name="ignore">if set to <c>true</c> [ignore].</param>
115 void IFluentMap.SqlIgnore(string propName, bool ignore)
116 {
117 var member = this.GetMemberExtension(propName);
118 member.Attributes.Add(Attributes.SqlIgnore.Ignore, this.ToString(ignore));
119 this.EachChilds(m => m.SqlIgnore(propName, ignore));
120 }
121
122 /// <summary>
123 /// Maps the ignore.
124 /// </summary>
125 /// <param name="propName">Name of the prop.</param>
126 /// <param name="ignore">if set to <c>true</c> [ignore].</param>
127 void IFluentMap.MapIgnore(string propName, bool ignore)
128 {
129 var member = this.GetMemberExtension(propName);
130 member.Attributes.Add(Attributes.MapIgnore.Ignore, this.ToString(ignore));
131 this.EachChilds(m => m.MapIgnore(propName, ignore));
132 }
133
134 /// <summary>
135 /// Trimmables the specified prop name.
136 /// </summary>
137 /// <param name="propName">Name of the prop.</param>
138 void IFluentMap.Trimmable(string propName)
139 {
140 var member = this.GetMemberExtension(propName);
141 member.Attributes.Add(Attributes.Trimmable, this.ToString(true));
142 this.EachChilds(m => m.Trimmable(propName));
143 }
144
145 /// <summary>
146 /// Maps the value.
147 /// </summary>
148 /// <typeparam name="TR">The type of the R.</typeparam>
149 /// <typeparam name="TV">The type of the V.</typeparam>
150 /// <param name="propName">Name of the prop.</param>
151 /// <param name="origValue">The orig value.</param>
152 /// <param name="value">The value.</param>
153 /// <param name="values">The values.</param>
154 void IFluentMap.MapValue<TR, TV>(string propName, TR origValue, TV value, TV[] values)
155 {
156 var member = this.GetMemberExtension(propName);
157 this.FillMapValueExtension(member.Attributes, origValue, value, values);
158 this.EachChilds(m => m.MapValue(propName, origValue, value, values));
159 }
160
161 /// <summary>
162 /// Maps the value.
163 /// </summary>
164 /// <typeparam name="TV">The type of the V.</typeparam>
165 /// <param name="origValue">The orig value.</param>
166 /// <param name="value">The value.</param>
167 /// <param name="values">The values.</param>
168 void IFluentMap.MapValue<TV>(Enum origValue, TV value, TV[] values)
169 {
170 MemberExtension member;
171 var name = Enum.GetName(origValue.GetType(), origValue);
172 if (!this._typeExtension.Members.TryGetValue(name, out member))
173 {
174 member = new MemberExtension { Name = name };
175 this._typeExtension.Members.Add(member);
176 }
177 this.FillMapValueExtension(member.Attributes, origValue, value, values);
178 this.EachChilds(m => m.MapValue(origValue, value, values));
179 }
180
181 /// <summary>
182 /// Maps the value.
183 /// </summary>
184 /// <typeparam name="TV">The type of the V.</typeparam>
185 /// <param name="origValue">The orig value.</param>
186 /// <param name="value">The value.</param>
187 /// <param name="values">The values.</param>
188 void IFluentMap.MapValue<TV>(object origValue, TV value, TV[] values)
189 {
190 this.FillMapValueExtension(this._typeExtension.Attributes, origValue, value, values);
191 this.EachChilds(m => m.MapValue(origValue, value, values));
192 }
193
194 /// <summary>
195 /// Defauls the value.
196 /// </summary>
197 /// <typeparam name="TR">The type of the R.</typeparam>
198 /// <param name="propName">Name of the prop.</param>
199 /// <param name="value">The value.</param>
200 void IFluentMap.DefaulValue<TR>(string propName, TR value)
201 {
202 var member = this.GetMemberExtension(propName);
203 member.Attributes.Add(Attributes.DefaultValue, Convert.ToString(value));
204 this.EachChilds(m => m.DefaulValue(propName, value));
205 }
206
207 /// <summary>
208 /// DB-Type of the value.
209 /// </summary>
210 /// <typeparam name="TR">The type of the R.</typeparam>
211 /// <param name="propName">Name of the prop.</param>
212 /// <param name="dbType">The value.</param>
213 void IFluentMap.DbType<TR>(string propName, DbType dbType)
214 {
215 var member = this.GetMemberExtension(propName);
216 member.Attributes.Add(Attributes.DbType, Convert.ToString(dbType));
217 this.EachChilds(m => m.DefaulValue(propName, dbType));
218 }
219
220 /// <summary>
221 /// MemberMapper
222 /// </summary>
223 /// <typeparam name="TR">The type of the R.</typeparam>
224 /// <param name="propName">Name of the prop.</param>
225 /// <param name="value">The value.</param>
226 void IFluentMap.MemberMapper<TR>(string propName, Type memberType, Type memberMapperType)
227 {
228 var member = this.GetMemberExtension(propName);
229 this.FillMemberMapperExtension(member.Attributes, memberType, memberMapperType);
230 this.EachChilds(m => m.MemberMapper<TR>(propName, memberType, memberMapperType));
231 }
232
233 /// <summary>
234 /// Nullables the specified prop name.
235 /// </summary>
236 /// <param name="propName">Name of the prop.</param>
237 /// <param name="isNullable">if set to <c>true</c> [is nullable].</param>
238 void IFluentMap.Nullable(string propName, bool isNullable)
239 {
240 var member = this.GetMemberExtension(propName);
241 member.Attributes.Add(Attributes.Nullable.IsNullable, this.ToString(isNullable));
242 this.EachChilds(m => m.Nullable(propName, isNullable));
243 }
244
245 void IFluentMap.LazyInstance(string propName, bool isLazy)
246 {
247 var member = this.GetMemberExtension(propName);
248 member.Attributes.Add(Attributes.LazyInstance.IsLazyInstance, this.ToString(isLazy));
249 this.EachChilds(m => m.LazyInstance(propName, isLazy));
250 }
251
252 /// <summary>
253 /// Nulls the value.
254 /// </summary>
255 /// <typeparam name="TR">The type of the R.</typeparam>
256 /// <param name="propName">Name of the prop.</param>
257 /// <param name="value">The value.</param>
258 void IFluentMap.NullValue<TR>(string propName, TR value)
259 {
260 var member = this.GetMemberExtension(propName);
261 member.Attributes.Add(Attributes.NullValue, Equals(value, null) ? null : Convert.ToString(value));
262 this.EachChilds(m => m.NullValue(propName, value));
263 }
264
265 /// <summary>
266 /// Associations the specified prop name.
267 /// </summary>
268 /// <param name="propName">Name of the prop.</param>
269 /// <param name="canBeNull">if set to <c>true</c> [can be null].</param>
270 /// <param name="thisKeys">The this keys.</param>
271 /// <param name="otherKeys">The other keys.</param>
272 void IFluentMap.Association(string propName, bool canBeNull, string thisKeys, string otherKeys)
273 {
274 var member = this.GetMemberExtension(propName);
275 AttributeExtensionCollection attrs;
276 if (!member.Attributes.TryGetValue(TypeExtension.NodeName.Association, out attrs))
277 {
278 attrs = new AttributeExtensionCollection();
279 member.Attributes.Add(TypeExtension.NodeName.Association, attrs);
280 }
281 attrs.Clear();
282 var attributeExtension = new AttributeExtension();
283 attributeExtension.Values.Add(Attributes.Association.ThisKey, thisKeys);
284 attributeExtension.Values.Add(Attributes.Association.OtherKey, otherKeys);
285 attributeExtension.Values.Add(Attributes.Association.Storage, this.ToString(canBeNull));
286 attrs.Add(attributeExtension);
287 this.EachChilds(m => m.Association(propName, canBeNull, thisKeys, otherKeys));
288 }
289
290 /// <summary>
291 /// Relations the specified prop name.
292 /// </summary>
293 /// <param name="propName">Name of the prop.</param>
294 /// <param name="destinationType">Type of the destination.</param>
295 /// <param name="slaveIndex">Index of the slave.</param>
296 /// <param name="masterIndex">Index of the master.</param>
297 void IFluentMap.Relation(string propName, Type destinationType, string[] slaveIndex, string[] masterIndex)
298 {
299 if (TypeHelper.IsSameOrParent(typeof(IEnumerable), destinationType))
300 {
301 destinationType = destinationType.GetGenericArguments().Single();
302 }
303 var member = this.GetMemberExtension(propName);
304 AttributeExtensionCollection attrs;
305 if (!member.Attributes.TryGetValue(TypeExtension.NodeName.Relation, out attrs))
306 {
307 attrs = new AttributeExtensionCollection();
308 member.Attributes.Add(TypeExtension.NodeName.Relation, attrs);
309 }
310 attrs.Clear();
311 var attributeExtension = new AttributeExtension();
312 attributeExtension.Values.Add(TypeExtension.AttrName.DestinationType, destinationType.AssemblyQualifiedName);
313 attrs.Add(attributeExtension);
314
315 FillRelationIndex(slaveIndex, attributeExtension, TypeExtension.NodeName.SlaveIndex);
316 FillRelationIndex(masterIndex, attributeExtension, TypeExtension.NodeName.MasterIndex);
317 this.EachChilds(m => m.Relation(propName, destinationType, slaveIndex, masterIndex));
318 }
319
320 /// <summary>
321 /// Inheritances the mapping.
322 /// </summary>
323 /// <param name="type">The type.</param>
324 /// <param name="code">The code.</param>
325 /// <param name="isDefault">The is default.</param>
326 void IFluentMap.InheritanceMapping(Type type, object code, bool? isDefault)
327 {
328 AttributeExtensionCollection extList;
329 if (!this._typeExtension.Attributes.TryGetValue(Attributes.InheritanceMapping.Name, out extList))
330 {
331 extList = new AttributeExtensionCollection();
332 this._typeExtension.Attributes.Add(Attributes.InheritanceMapping.Name, extList);
333 }
334 var attr = new AttributeExtension();
335 attr.Values.Add(Attributes.InheritanceMapping.Type, type.AssemblyQualifiedName);
336 if (null != code)
337 {
338 attr.Values.Add(Attributes.InheritanceMapping.Code, code);
339 }
340 if (null != isDefault)
341 {
342 attr.Values.Add(Attributes.InheritanceMapping.IsDefault, isDefault.Value);
343 }
344 extList.Add(attr);
345 this.EachChilds(m => m.InheritanceMapping(type,code,isDefault));
346 }
347 }
348 }