Skip to content

Commit

Permalink
Bug/7 non scalar query in dapper requires access to dbtransaction (#8)
Browse files Browse the repository at this point in the history
* Create test to identify problem with Dapper non-scalar transactional reads.

* Expose transaction for use with Dapper

Note this transaction object should not be used to commit or rollback the transaction!
  • Loading branch information
gentoorax committed Jan 15, 2024
1 parent 3e08ac6 commit 8a0d5be
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void TestInitialize()
simpleTestRepository.DeleteAll();
}

[TestCategory("IntegrationTests"), TestMethod]
public void MssqlAdoScopeBasicReadRecordsTest()
{
using IAdoScope adoScope = adoScopeFactory.Create();
Assert.IsFalse(simpleTestRepository.GetEntities().Any());
adoScope.Complete();
}

[TestCategory("IntegrationTests"), TestMethod]
public void MssqlAdoScopeBasicTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface ISimpleMssqlTestRepository

TestEntity GetEntityByName(string name);

IEnumerable<TestEntity> GetEntities();

void AddWithDifferentContext(TestEntity entity);

int GetEntityCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Promethix.Framework.Ado.Enums;
using Promethix.Framework.Ado.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Promethix.Framework.Ado.Tests.TestSupport.DataAccess.Mssql
{
public class MssqlContextExample3 : AdoContext
{
public MssqlContextExample3()
: base(
"MssqlContextExample3",
"Microsoft.Data.SqlClient",
"Server=(local);Database=AdoScopeTest1;Integrated Security=True;TrustServerCertificate=True",
AdoContextExecutionOption.Transactional)
{
// No Implementation
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public SimpleMssqlTestRepository(IAmbientAdoContextLocator ambientAdoContextLoca

private IDbConnection SqlConnection2 => ambientAdoContextLocator.GetContext<MssqlContextExample2>().Connection;

private IDbConnection SqlConnection3 => ambientAdoContextLocator.GetContext<MssqlContextExample3>().Connection;

private IDbTransaction SqlTransaction3 => ambientAdoContextLocator.GetContext<MssqlContextExample3>().Transaction;

public void Add(TestEntity entity)
{
const string query = "INSERT INTO TestEntity (Name, Description, Quantity) VALUES (@Name, @Description, @Quantity)";
Expand All @@ -42,6 +46,12 @@ public TestEntity GetEntityByName(string name)
return SqlConnection1.QuerySingleOrDefault<TestEntity>(query, new { Name = name });
}

public IEnumerable<TestEntity> GetEntities()
{
const string query = "SELECT * FROM TestEntity";
return SqlConnection3.Query<TestEntity>(query, transaction: SqlTransaction3);
}

public int GetEntityCount()
{
const string query = "SELECT COUNT(*) FROM TestEntity";
Expand Down
17 changes: 17 additions & 0 deletions Promethix.Framework.Ado/Implementation/AdoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public IDbConnection Connection
}
}

/// <summary>
/// Gets the transaction for reference.
/// WARNING: This should not be used to commit or rollback the transaction!
/// </summary>
public IDbTransaction Transaction
{
get
{
if (transaction == null)
{
throw new InvalidOperationException("There is no active transaction.");
}

return transaction;
}
}

protected AdoContext()
{
// No Implementation
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build and Test 0.1.x-alpha](https://github.com/gentoorax/Promethix.Framework.Ado/actions/workflows/adoscope-nuget-build.yaml/badge.svg)](https://github.com/gentoorax/Promethix.Framework.Ado/actions/workflows/adoscope-nuget-build.yaml)
[![Published to nuget.org 0.1.x-x-alpha](https://github.com/gentoorax/Promethix.Framework.Ado/actions/workflows/adoscope-nuget-publish-prerelease.yaml/badge.svg)](https://github.com/gentoorax/Promethix.Framework.Ado/actions/workflows/adoscope-nuget-publish-prerelease.yaml)

**Recently promoted to v1.0.0-rc2. All major features have been implemented.**
**Recently promoted to v1.0.0-rc3. All major features have been implemented.**

**Now incldues .NET 8.0 support**

Expand Down

0 comments on commit 8a0d5be

Please sign in to comment.