|
1 |
| -using System.Collections; |
2 |
| -using System.Collections.Generic; |
3 |
| -using FluentNHibernate.Testing.Values; |
4 |
| -using FluentNHibernate.Utils; |
5 |
| -using NHibernate; |
6 |
| - |
7 |
| -namespace FluentNHibernate.Testing |
8 |
| -{ |
9 |
| - public class PersistenceSpecification<T> |
10 |
| - { |
11 |
| - protected readonly List<Property<T>> allProperties = new List<Property<T>>(); |
12 |
| - private readonly ISession currentSession; |
13 |
| - private readonly IEqualityComparer entityEqualityComparer; |
14 |
| - private readonly bool hasExistingSession; |
15 |
| - |
16 |
| - public PersistenceSpecification(ISessionSource source) |
17 |
| - : this(source.CreateSession()) |
18 |
| - { |
19 |
| - } |
20 |
| - |
21 |
| - public PersistenceSpecification(ISessionSource source, IEqualityComparer entityEqualityComparer) |
22 |
| - : this(source.CreateSession(), entityEqualityComparer) |
23 |
| - { |
24 |
| - } |
25 |
| - |
26 |
| - public PersistenceSpecification(ISession session) |
27 |
| - : this(session, null) |
28 |
| - { |
29 |
| - } |
30 |
| - |
31 |
| - public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer) |
32 |
| - { |
33 |
| - currentSession = session; |
34 |
| - hasExistingSession = currentSession.Transaction != null && currentSession.Transaction.IsActive; |
35 |
| - this.entityEqualityComparer = entityEqualityComparer; |
36 |
| - } |
37 |
| - |
38 |
| - public T VerifyTheMappings() |
39 |
| - { |
40 |
| - return VerifyTheMappings(typeof(T).InstantiateUsingParameterlessConstructor<T>()); |
41 |
| - } |
42 |
| - |
43 |
| - public T VerifyTheMappings(T first) |
44 |
| - { |
45 |
| - // Set the "suggested" properties, including references |
46 |
| - // to other entities and possibly collections |
47 |
| - allProperties.ForEach(p => p.SetValue(first)); |
48 |
| - |
49 |
| - // Save the first copy |
50 |
| - TransactionalSave(first); |
51 |
| - |
52 |
| - object firstId = currentSession.GetIdentifier(first); |
53 |
| - |
54 |
| - // Clear and reset the current session |
55 |
| - currentSession.Flush(); |
56 |
| - currentSession.Clear(); |
57 |
| - |
58 |
| - // "Find" the same entity from the second IRepository |
59 |
| - var second = currentSession.Get<T>(firstId); |
60 |
| - |
61 |
| - // Validate that each specified property and value |
62 |
| - // made the round trip |
63 |
| - // It's a bit naive right now because it fails on the first failure |
64 |
| - allProperties.ForEach(p => p.CheckValue(second)); |
65 |
| - |
66 |
| - return second; |
67 |
| - } |
68 |
| - |
69 |
| - public void TransactionalSave(object propertyValue) |
70 |
| - { |
71 |
| - if (hasExistingSession) |
72 |
| - { |
73 |
| - currentSession.Save(propertyValue); |
74 |
| - } |
75 |
| - else |
76 |
| - { |
77 |
| - using (var tx = currentSession.BeginTransaction()) |
78 |
| - { |
79 |
| - currentSession.Save(propertyValue); |
80 |
| - tx.Commit(); |
81 |
| - } |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property) |
86 |
| - { |
87 |
| - return RegisterCheckedProperty(property, null); |
88 |
| - } |
89 |
| - |
90 |
| - public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property, IEqualityComparer equalityComparer) |
91 |
| - { |
92 |
| - property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer; |
93 |
| - allProperties.Add(property); |
94 |
| - |
95 |
| - property.HasRegistered(this); |
96 |
| - |
97 |
| - return this; |
98 |
| - } |
99 |
| - } |
100 |
| -} |
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using FluentNHibernate.Testing.Values; |
| 4 | +using FluentNHibernate.Utils; |
| 5 | +using NHibernate; |
| 6 | + |
| 7 | +namespace FluentNHibernate.Testing |
| 8 | +{ |
| 9 | + public class PersistenceSpecification<T> |
| 10 | + { |
| 11 | + protected readonly List<Property<T>> allProperties = new List<Property<T>>(); |
| 12 | + private readonly ISession currentSession; |
| 13 | + private readonly IEqualityComparer entityEqualityComparer; |
| 14 | + private readonly bool hasExistingTransaction; |
| 15 | + |
| 16 | + public PersistenceSpecification(ISessionSource source) |
| 17 | + : this(source.CreateSession()) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + public PersistenceSpecification(ISessionSource source, IEqualityComparer entityEqualityComparer) |
| 22 | + : this(source.CreateSession(), entityEqualityComparer) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public PersistenceSpecification(ISession session) |
| 27 | + : this(session, null) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer) |
| 32 | + { |
| 33 | + currentSession = session; |
| 34 | + hasExistingTransaction = currentSession.Transaction != null && currentSession.Transaction.IsActive || System.Transactions.Transaction.Current != null; |
| 35 | + this.entityEqualityComparer = entityEqualityComparer; |
| 36 | + } |
| 37 | + |
| 38 | + public T VerifyTheMappings() |
| 39 | + { |
| 40 | + return VerifyTheMappings(typeof(T).InstantiateUsingParameterlessConstructor<T>()); |
| 41 | + } |
| 42 | + |
| 43 | + public T VerifyTheMappings(T first) |
| 44 | + { |
| 45 | + // Set the "suggested" properties, including references |
| 46 | + // to other entities and possibly collections |
| 47 | + allProperties.ForEach(p => p.SetValue(first)); |
| 48 | + |
| 49 | + // Save the first copy |
| 50 | + TransactionalSave(first); |
| 51 | + |
| 52 | + object firstId = currentSession.GetIdentifier(first); |
| 53 | + |
| 54 | + // Clear and reset the current session |
| 55 | + currentSession.Flush(); |
| 56 | + currentSession.Clear(); |
| 57 | + |
| 58 | + // "Find" the same entity from the second IRepository |
| 59 | + var second = currentSession.Get<T>(firstId); |
| 60 | + |
| 61 | + // Validate that each specified property and value |
| 62 | + // made the round trip |
| 63 | + // It's a bit naive right now because it fails on the first failure |
| 64 | + allProperties.ForEach(p => p.CheckValue(second)); |
| 65 | + |
| 66 | + return second; |
| 67 | + } |
| 68 | + |
| 69 | + public void TransactionalSave(object propertyValue) |
| 70 | + { |
| 71 | + if (hasExistingTransaction) |
| 72 | + { |
| 73 | + currentSession.Save(propertyValue); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + using (var tx = currentSession.BeginTransaction()) |
| 78 | + { |
| 79 | + currentSession.Save(propertyValue); |
| 80 | + tx.Commit(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property) |
| 86 | + { |
| 87 | + return RegisterCheckedProperty(property, null); |
| 88 | + } |
| 89 | + |
| 90 | + public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property, IEqualityComparer equalityComparer) |
| 91 | + { |
| 92 | + property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer; |
| 93 | + allProperties.Add(property); |
| 94 | + |
| 95 | + property.HasRegistered(this); |
| 96 | + |
| 97 | + return this; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments