fluent-nhibernate /src/FluentNHibernate/Testing/PersistenceSpecification.cs

Language C# Lines 92
MD5 Hash d4e1f7a4dcd8e1ef81a74c728a8b8bd2
Repository https://github.com/dotnetchris/fluent-nhibernate.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using FluentNHibernate.Testing.Values;
using FluentNHibernate.Utils;
using NHibernate;

namespace FluentNHibernate.Testing
{
    public class PersistenceSpecification<T>
    {
        protected readonly List<Property<T>> allProperties = new List<Property<T>>();
        private readonly ISession currentSession;
        private readonly IEqualityComparer entityEqualityComparer;
        private readonly bool hasExistingSession;

        public PersistenceSpecification(ISessionSource source)
            : this(source.CreateSession())
        {
        }

        public PersistenceSpecification(ISessionSource source, IEqualityComparer entityEqualityComparer)
            : this(source.CreateSession(), entityEqualityComparer)
        {
        }

        public PersistenceSpecification(ISession session)
            : this(session, null)
        {
        }

        public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer)
        {
            currentSession = session;
            hasExistingSession = currentSession.Transaction != null && currentSession.Transaction.IsActive;
            this.entityEqualityComparer = entityEqualityComparer;
        }

        public void VerifyTheMappings()
        {
            // CreateProperties the initial copy
            var first = typeof(T).InstantiateUsingParameterlessConstructor<T>();

            // Set the "suggested" properties, including references
            // to other entities and possibly collections
            allProperties.ForEach(p => p.SetValue(first));

            // Save the first copy
            TransactionalSave(first);

            object firstId = currentSession.GetIdentifier(first);

            // Clear and reset the current session
            currentSession.Flush();
            currentSession.Clear();

            // "Find" the same entity from the second IRepository
            var second = currentSession.Get<T>(firstId);

            // Validate that each specified property and value
            // made the round trip
            // It's a bit naive right now because it fails on the first failure
            allProperties.ForEach(p => p.CheckValue(second));
        }

        public void TransactionalSave(object propertyValue)
        {
            if (hasExistingSession)
            {
                currentSession.Save(propertyValue);
            }
            else
            {
                using (var tx = currentSession.BeginTransaction())
                {
                    currentSession.Save(propertyValue);
                    tx.Commit();
                }
            }
        }

        public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property)
        {
            property.EntityEqualityComparer = entityEqualityComparer;
            allProperties.Add(property);

            property.HasRegistered(this);

            return this;
        }
    }
}
Back to Top