Skip to content

Commit a30329e

Browse files
authored
Stop using obsolete ISession.Transaction property (#693)
+semver:feature
1 parent 257dd8a commit a30329e

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/FluentNHibernate.Testing/Testing/PersistenceSpecificationTransactionTest.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using FakeItEasy;
33
using NHibernate;
4+
using NHibernate.Engine;
45
using NUnit.Framework;
56

67
namespace FluentNHibernate.Testing.Testing;
@@ -23,44 +24,44 @@ class CatType
2324
[Test]
2425
public void Should_Not_Open_A_New_Transaction_If_One_Is_Passed()
2526
{
26-
var transaction = A.Fake<ITransaction>();
27-
A.CallTo(() => transaction.IsActive).Returns(true);
27+
var sessionImpl = A.Fake<ISessionImplementor>();
28+
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(true);
2829

2930
var session = A.Fake<ISession>();
30-
A.CallTo(() => session.Transaction).Returns(transaction);
31+
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);
3132

3233
var spec = new PersistenceSpecification<Cat>(session);
3334
spec.VerifyTheMappings();
3435

3536
A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened();
36-
A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
3737
}
3838

3939
[Test]
4040
public void Should_Open_A_New_Transaction_If_None_Is_Passed()
4141
{
42-
43-
var transaction = A.Fake<ITransaction>();
44-
A.CallTo(() => transaction.IsActive).Returns(false);
42+
var sessionImpl = A.Fake<ISessionImplementor>();
43+
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(false);
4544

4645
var session = A.Fake<ISession>();
47-
A.CallTo(() => session.Transaction).Returns(transaction);
48-
A.CallTo(() => session.BeginTransaction()).Returns(transaction);
46+
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);
47+
A.CallTo(() => session.BeginTransaction()).Returns(A.Dummy<ITransaction>());
4948

5049
var spec = new PersistenceSpecification<Cat>(session);
5150
spec.VerifyTheMappings();
5251

53-
A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
52+
A.CallTo(() => session.BeginTransaction()).MustHaveHappened(1, Times.Exactly);
5453
}
5554

5655
[Test]
5756
public void Passed_Transaction_Should_Apply_For_Reference_Saving()
5857
{
59-
var transaction = A.Fake<ITransaction>();
60-
A.CallTo(() => transaction.IsActive).Returns(true);
58+
var sessionImpl = A.Fake<ISessionImplementor>();
59+
A.CallTo(() => sessionImpl.TransactionInProgress).Returns(true);
6160

6261
var session = A.Fake<ISession>();
63-
A.CallTo(() => session.Transaction).Returns(transaction);
62+
A.CallTo(() => session.GetSessionImplementation()).Returns(sessionImpl);
63+
A.CallTo(() => session.BeginTransaction()).Returns(A.Dummy<ITransaction>());
64+
6465
A.CallTo(() => session.Get<Cat>(null)).WithAnyArguments()
6566
.Returns(
6667
new Cat
@@ -79,7 +80,6 @@ public void Passed_Transaction_Should_Apply_For_Reference_Saving()
7980

8081
spec.VerifyTheMappings();
8182

82-
A.CallTo(() => session.Transaction).MustHaveHappened(2, Times.Exactly);
8383
A.CallTo(() => session.BeginTransaction()).MustNotHaveHappened();
8484
}
8585

@@ -90,9 +90,9 @@ bool IEqualityComparer.Equals(object x, object y)
9090
if (x is null || y is null)
9191
return false;
9292

93-
if (x.GetType().GetProperty("Name") is not null && y.GetType().GetProperty("Name") is not null)
93+
if (x.GetType().GetProperty("Name") is { } xProp && y.GetType().GetProperty("Name") is { } yProp)
9494
{
95-
return x.GetType().GetProperty("Name").GetValue(x, null) == y.GetType().GetProperty("Name").GetValue(x, null);
95+
return xProp.GetValue(x, null) == yProp.GetValue(x, null);
9696
}
9797

9898
return x.Equals(y);

src/FluentNHibernate/Testing/PersistenceSpecification.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public PersistenceSpecification(ISession session)
3131
public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer)
3232
{
3333
currentSession = session;
34-
#pragma warning disable CS0618 // ISession.Transaction is obsolete
35-
hasExistingTransaction = currentSession.Transaction is not null && currentSession.Transaction.IsActive || System.Transactions.Transaction.Current is not null;
36-
#pragma warning restore CS0618
34+
hasExistingTransaction = currentSession.GetSessionImplementation().TransactionInProgress;
3735
this.entityEqualityComparer = entityEqualityComparer;
3836
}
3937

0 commit comments

Comments
 (0)