Mercurial > pub > bltoolkit
comparison UnitTests/Linq/Model/ParentChild.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.Generic; | |
3 using System.Reflection; | |
4 | |
5 using BLToolkit.Data.Linq; | |
6 using BLToolkit.DataAccess; | |
7 using BLToolkit.Mapping; | |
8 | |
9 namespace Data.Linq.Model | |
10 { | |
11 #region Parent/Child/GrandChild | |
12 | |
13 public interface IParent | |
14 { | |
15 int ParentID { get; } | |
16 int? Value1 { get; } | |
17 } | |
18 | |
19 public class Parent : IEquatable<Parent>, IComparable | |
20 { | |
21 public int ParentID; | |
22 public int? Value1; | |
23 | |
24 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
25 public List<Child> Children; | |
26 | |
27 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
28 public List<GrandChild> GrandChildren; | |
29 | |
30 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
31 public IEnumerable<Child> Children2 | |
32 { | |
33 get { return Children; } | |
34 } | |
35 | |
36 public override bool Equals(object obj) | |
37 { | |
38 if (obj.GetType() != typeof(Parent)) return false; | |
39 return Equals((Parent)obj); | |
40 } | |
41 | |
42 public bool Equals(Parent other) | |
43 { | |
44 if (ReferenceEquals(null, other)) return false; | |
45 if (ReferenceEquals(this, other)) return true; | |
46 return other.ParentID == ParentID && other.Value1.Equals(Value1); | |
47 } | |
48 | |
49 public override int GetHashCode() | |
50 { | |
51 unchecked { return (ParentID * 397) ^ (Value1 ?? 0); } | |
52 } | |
53 | |
54 public int CompareTo(object obj) | |
55 { | |
56 return ParentID - ((Parent)obj).ParentID; | |
57 } | |
58 | |
59 [Association(ThisKey = "ParentID", OtherKey = "ID")] | |
60 public LinqDataTypes Types; | |
61 } | |
62 | |
63 public class Child | |
64 { | |
65 [PrimaryKey] public int ParentID; | |
66 [PrimaryKey] public int ChildID; | |
67 | |
68 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
69 public Parent Parent; | |
70 | |
71 [Association(ThisKey = "ParentID", OtherKey = "ParentID", CanBeNull = false)] | |
72 public Parent1 Parent1; | |
73 | |
74 [Association(ThisKey = "ParentID", OtherKey = "ParentID2", CanBeNull = false)] | |
75 public Parent3 ParentID2; | |
76 | |
77 [Association(ThisKey = "ParentID, ChildID", OtherKey = "ParentID, ChildID")] | |
78 public List<GrandChild> GrandChildren; | |
79 | |
80 public override bool Equals(object obj) | |
81 { | |
82 return Equals(obj as Child); | |
83 } | |
84 | |
85 public bool Equals(Child other) | |
86 { | |
87 if (ReferenceEquals(null, other)) return false; | |
88 if (ReferenceEquals(this, other)) return true; | |
89 | |
90 return other.ParentID == ParentID && other.ChildID == ChildID; | |
91 } | |
92 | |
93 public override int GetHashCode() | |
94 { | |
95 unchecked { return (ParentID * 397) ^ ChildID; } | |
96 } | |
97 } | |
98 | |
99 public class GrandChild : IEquatable<GrandChild> | |
100 { | |
101 public GrandChild() | |
102 { | |
103 | |
104 } | |
105 | |
106 public int? ParentID; | |
107 public int? ChildID; | |
108 public int? GrandChildID; | |
109 | |
110 [Association(ThisKey = "ParentID, ChildID", OtherKey = "ParentID, ChildID")] | |
111 public Child Child; | |
112 | |
113 public override bool Equals(object obj) | |
114 { | |
115 if (ReferenceEquals(null, obj)) return false; | |
116 if (ReferenceEquals(this, obj)) return true; | |
117 if (obj.GetType() != typeof (GrandChild)) return false; | |
118 | |
119 return Equals((GrandChild)obj); | |
120 } | |
121 | |
122 public bool Equals(GrandChild other) | |
123 { | |
124 if (ReferenceEquals(null, other)) return false; | |
125 if (ReferenceEquals(this, other)) return true; | |
126 | |
127 return other.ParentID.Equals(ParentID) && other.ChildID.Equals(ChildID) && other.GrandChildID.Equals(GrandChildID); | |
128 } | |
129 | |
130 public override int GetHashCode() | |
131 { | |
132 unchecked | |
133 { | |
134 var result = ParentID.HasValue ? ParentID.Value : 0; | |
135 | |
136 result = (result * 397) ^ (ChildID. HasValue ? ChildID. Value : 0); | |
137 result = (result * 397) ^ (GrandChildID.HasValue ? GrandChildID.Value : 0); | |
138 | |
139 return result; | |
140 } | |
141 } | |
142 } | |
143 | |
144 [TableName("Parent")] | |
145 public class Parent3 : IEquatable<Parent3>, IComparable | |
146 { | |
147 [MapField("ParentID")] | |
148 public int ParentID2 { get; set; } | |
149 public int? Value1 { get; set; } | |
150 | |
151 public override bool Equals(object obj) | |
152 { | |
153 if (obj.GetType() != typeof (Parent3)) return false; | |
154 return Equals((Parent3)obj); | |
155 } | |
156 | |
157 public bool Equals(Parent3 other) | |
158 { | |
159 if (ReferenceEquals(null, other)) return false; | |
160 if (ReferenceEquals(this, other)) return true; | |
161 return other.ParentID2 == ParentID2 && other.Value1.Equals(Value1); | |
162 } | |
163 | |
164 public override int GetHashCode() | |
165 { | |
166 unchecked { return (ParentID2 * 397) ^ (Value1 ?? 0); } | |
167 } | |
168 | |
169 public int CompareTo(object obj) | |
170 { | |
171 return ParentID2 - ((Parent3)obj).ParentID2; | |
172 } | |
173 } | |
174 | |
175 [TableName("Parent")] | |
176 public class Parent4 : IEquatable<Parent4>, IComparable | |
177 { | |
178 public int ParentID; | |
179 public TypeValue Value1; | |
180 | |
181 public override bool Equals(object obj) | |
182 { | |
183 if (obj.GetType() != typeof (Parent4)) return false; | |
184 return Equals((Parent4)obj); | |
185 } | |
186 | |
187 public bool Equals(Parent4 other) | |
188 { | |
189 if (ReferenceEquals(null, other)) return false; | |
190 if (ReferenceEquals(this, other)) return true; | |
191 return other.ParentID == ParentID && other.Value1.Equals(Value1); | |
192 } | |
193 | |
194 public override int GetHashCode() | |
195 { | |
196 unchecked { return (ParentID * 397) ^ (int)Value1; } | |
197 } | |
198 | |
199 public int CompareTo(object obj) | |
200 { | |
201 return ParentID - ((Parent4)obj).ParentID; | |
202 } | |
203 } | |
204 | |
205 [TableName("Parent")] | |
206 public class Parent5 : IEquatable<Parent5>, IComparable | |
207 { | |
208 public int ParentID; | |
209 public int? Value1; | |
210 | |
211 [Association(ThisKey = "ParentID", OtherKey = "Value1", CanBeNull = true)] | |
212 public List<Parent5> Children; | |
213 | |
214 public override bool Equals(object obj) | |
215 { | |
216 if (obj.GetType() != typeof(Parent5)) return false; | |
217 return Equals((Parent5)obj); | |
218 } | |
219 | |
220 public bool Equals(Parent5 other) | |
221 { | |
222 if (ReferenceEquals(null, other)) return false; | |
223 if (ReferenceEquals(this, other)) return true; | |
224 return other.ParentID == ParentID && other.Value1.Equals(Value1); | |
225 } | |
226 | |
227 public override int GetHashCode() | |
228 { | |
229 unchecked { return (ParentID * 397) ^ (int)Value1; } | |
230 } | |
231 | |
232 public int CompareTo(object obj) | |
233 { | |
234 return ParentID - ((Parent5)obj).ParentID; | |
235 } | |
236 } | |
237 | |
238 #endregion | |
239 | |
240 #region Parent1/GrandChild1 | |
241 | |
242 [TableName("Parent")] | |
243 public class Parent1 : IEquatable<Parent1>, IComparable | |
244 { | |
245 [PrimaryKey] | |
246 public int ParentID; | |
247 public int? Value1; | |
248 | |
249 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
250 public List<Child> Children; | |
251 | |
252 public override bool Equals(object obj) | |
253 { | |
254 if (obj.GetType() != typeof (Parent1)) return false; | |
255 return Equals((Parent1)obj); | |
256 } | |
257 | |
258 public bool Equals(Parent1 other) | |
259 { | |
260 if (ReferenceEquals(null, other)) return false; | |
261 if (ReferenceEquals(this, other)) return true; | |
262 return other.ParentID == ParentID; | |
263 } | |
264 | |
265 public override int GetHashCode() | |
266 { | |
267 unchecked { return ParentID * 397; } | |
268 } | |
269 | |
270 public int CompareTo(object obj) | |
271 { | |
272 return ParentID - ((Parent1)obj).ParentID; | |
273 } | |
274 } | |
275 | |
276 [TableName("GrandChild")] | |
277 public class GrandChild1 : IEquatable<GrandChild1> | |
278 { | |
279 public int ParentID; | |
280 public int? ChildID; | |
281 public int? GrandChildID; | |
282 | |
283 [Association(ThisKey = "ParentID, ChildID", OtherKey = "ParentID, ChildID")] | |
284 public Child Child; | |
285 | |
286 [Association(ThisKey = "ParentID", OtherKey = "ParentID", CanBeNull = false)] | |
287 public Parent1 Parent; | |
288 | |
289 public override bool Equals(object obj) | |
290 { | |
291 if (ReferenceEquals(null, obj)) return false; | |
292 if (ReferenceEquals(this, obj)) return true; | |
293 if (obj.GetType() != typeof (GrandChild1)) return false; | |
294 | |
295 return Equals((GrandChild1)obj); | |
296 } | |
297 | |
298 public bool Equals(GrandChild1 other) | |
299 { | |
300 if (ReferenceEquals(null, other)) return false; | |
301 if (ReferenceEquals(this, other)) return true; | |
302 | |
303 return other.ParentID.Equals(ParentID) && other.ChildID.Equals(ChildID) && other.GrandChildID.Equals(GrandChildID); | |
304 } | |
305 | |
306 public override int GetHashCode() | |
307 { | |
308 unchecked | |
309 { | |
310 var result = ParentID; | |
311 | |
312 result = (result * 397) ^ (ChildID. HasValue ? ChildID. Value : 0); | |
313 result = (result * 397) ^ (GrandChildID.HasValue ? GrandChildID.Value : 0); | |
314 | |
315 return result; | |
316 } | |
317 } | |
318 } | |
319 | |
320 #endregion | |
321 | |
322 #region Inheritance | |
323 | |
324 [TableName("Parent")] | |
325 [InheritanceMapping(Code = null, Type = typeof(ParentInheritanceNull))] | |
326 [InheritanceMapping(Code = 1, Type = typeof(ParentInheritance1))] | |
327 [InheritanceMapping( Type = typeof(ParentInheritanceValue), IsDefault = true)] | |
328 public abstract class ParentInheritanceBase : IEquatable<ParentInheritanceBase>, IComparable | |
329 { | |
330 [PrimaryKey] | |
331 public int ParentID; | |
332 | |
333 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
334 public List<Child> Children; | |
335 | |
336 public override bool Equals(object obj) | |
337 { | |
338 if (obj.GetType() != GetType()) return false; | |
339 return Equals((ParentInheritanceBase)obj); | |
340 } | |
341 | |
342 public bool Equals(ParentInheritanceBase other) | |
343 { | |
344 if (ReferenceEquals(null, other)) return false; | |
345 if (ReferenceEquals(this, other)) return true; | |
346 return other.ParentID == ParentID; | |
347 } | |
348 | |
349 public override int GetHashCode() | |
350 { | |
351 return ParentID; | |
352 } | |
353 | |
354 public int CompareTo(object obj) | |
355 { | |
356 return ParentID - ((Parent)obj).ParentID; | |
357 } | |
358 } | |
359 | |
360 public class ParentInheritanceNull : ParentInheritanceBase | |
361 { | |
362 } | |
363 | |
364 public class ParentInheritance1 : ParentInheritanceBase, IEquatable<ParentInheritance1> | |
365 { | |
366 [MapField(IsInheritanceDiscriminator = true)] | |
367 public int Value1; | |
368 | |
369 public override bool Equals(object obj) | |
370 { | |
371 var ret = base.Equals(obj) && Equals((ParentInheritance1)obj); | |
372 return ret; | |
373 } | |
374 | |
375 public bool Equals(ParentInheritance1 other) | |
376 { | |
377 return base.Equals(other) && other.Value1.Equals(Value1); | |
378 } | |
379 | |
380 public override int GetHashCode() | |
381 { | |
382 unchecked { return (ParentID * 397) ^ Value1; } | |
383 } | |
384 } | |
385 | |
386 public class ParentInheritanceValue : ParentInheritanceBase | |
387 { | |
388 [MapField(IsInheritanceDiscriminator = true)] | |
389 public int Value1; | |
390 | |
391 public override bool Equals(object obj) | |
392 { | |
393 return base.Equals(obj) && Equals((ParentInheritanceValue)obj); | |
394 } | |
395 | |
396 public bool Equals(ParentInheritanceValue other) | |
397 { | |
398 return base.Equals(other) && other.Value1.Equals(Value1); | |
399 } | |
400 | |
401 public override int GetHashCode() | |
402 { | |
403 unchecked { return (ParentID * 397) ^ Value1; } | |
404 } | |
405 } | |
406 | |
407 #endregion | |
408 | |
409 #region Inheritance2 | |
410 | |
411 [TableName("Parent")] | |
412 [InheritanceMapping(Code = null, Type = typeof(ParentInheritanceBase2))] | |
413 [InheritanceMapping(Code = 1, Type = typeof(ParentInheritance12))] | |
414 [InheritanceMapping(Code = 2, Type = typeof(ParentInheritance12))] | |
415 public abstract class ParentInheritanceBase2 | |
416 { | |
417 [PrimaryKey] | |
418 public int ParentID; | |
419 | |
420 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
421 public List<Child> Children; | |
422 } | |
423 | |
424 public class ParentInheritance12 : ParentInheritanceBase2 | |
425 { | |
426 [MapField(IsInheritanceDiscriminator = true)] | |
427 public int Value1; | |
428 } | |
429 | |
430 #endregion | |
431 | |
432 #region Inheritance3 | |
433 | |
434 [TableName("Parent")] | |
435 [InheritanceMapping(Code = null, Type = typeof(ParentInheritanceBase3))] | |
436 [InheritanceMapping(Code = 1, Type = typeof(ParentInheritance13))] | |
437 [InheritanceMapping(Code = 2, Type = typeof(ParentInheritance13))] | |
438 public abstract class ParentInheritanceBase3 | |
439 { | |
440 [PrimaryKey] | |
441 public int ParentID; | |
442 | |
443 [Association(ThisKey = "ParentID", OtherKey = "ParentID")] | |
444 public List<Child> Children; | |
445 } | |
446 | |
447 public class ParentInheritance13 : ParentInheritanceBase3 | |
448 { | |
449 [MapField("Value1", IsInheritanceDiscriminator = true)] | |
450 public int Value; | |
451 } | |
452 | |
453 #endregion | |
454 | |
455 #region Inheritance4 | |
456 | |
457 public enum Parent4Type | |
458 { | |
459 Value1 = 1, | |
460 Value2 = 2 | |
461 } | |
462 | |
463 [TableName("Parent")] | |
464 [InheritanceMapping(Code = (int)Parent4Type.Value1, Type = typeof(ParentInheritance14))] | |
465 [InheritanceMapping(Code = (int)Parent4Type.Value2, Type = typeof(ParentInheritance24))] | |
466 public abstract class ParentInheritanceBase4 | |
467 { | |
468 [PrimaryKey] | |
469 public int ParentID; | |
470 | |
471 public abstract Parent4Type Value1 { get; } | |
472 } | |
473 | |
474 public class ParentInheritance14 : ParentInheritanceBase4 | |
475 { | |
476 [MapField(IsInheritanceDiscriminator = true)] | |
477 public override Parent4Type Value1 { get { return Parent4Type.Value1; } } | |
478 } | |
479 | |
480 public class ParentInheritance24 : ParentInheritanceBase4 | |
481 { | |
482 [MapField(IsInheritanceDiscriminator = true)] | |
483 public override Parent4Type Value1 { get { return Parent4Type.Value2; } } | |
484 } | |
485 | |
486 #endregion | |
487 | |
488 public class Functions | |
489 { | |
490 private readonly IDataContext _ctx; | |
491 | |
492 public Functions(IDataContext ctx) | |
493 { | |
494 _ctx = ctx; | |
495 } | |
496 | |
497 [TableFunction(Name="GetParentByID")] | |
498 public Table<Parent> GetParentByID(int? id) | |
499 { | |
500 return _ctx.GetTable<Parent>(this, (MethodInfo)(MethodBase.GetCurrentMethod()), id); | |
501 } | |
502 | |
503 [TableExpression("{0} {1} WITH (TABLOCK)")] | |
504 public Table<T> WithTabLock<T>() | |
505 where T : class | |
506 { | |
507 return _ctx.GetTable<T>(this, ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(T))); | |
508 } | |
509 } | |
510 } |