Skip to content

Commit beeb422

Browse files
committed
- ActiveDatabaseContext
1 parent cab8fd4 commit beeb422

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Data;
2+
using Moq;
3+
using NUnit.Framework;
4+
5+
namespace Shuttle.Core.Data.Tests
6+
{
7+
public class DatabaseContextCacheFixture : Fixture
8+
{
9+
[Test]
10+
public void Should_be_able_to_use_different_contexts()
11+
{
12+
var cache = new DatabaseContextCache();
13+
14+
DatabaseContext.Assign(cache);
15+
16+
var context1 = new DatabaseContext("mock-1", new Mock<IDbConnection>().Object, new Mock<IDbCommandFactory>().Object).WithName("mock-1");
17+
18+
Assert.That(cache.Current.Key, Is.EqualTo(context1.Key));
19+
20+
var context2 = new DatabaseContext("mock-2", new Mock<IDbConnection>().Object, new Mock<IDbCommandFactory>().Object).WithName("mock-2");
21+
22+
Assert.That(cache.Current.Key, Is.EqualTo(context2.Key));
23+
24+
using (cache.Use("mock-1"))
25+
{
26+
Assert.That(cache.Current.Key, Is.EqualTo(context1.Key));
27+
}
28+
29+
Assert.That(cache.Current.Key, Is.EqualTo(context2.Key));
30+
31+
using (cache.Use(context1))
32+
{
33+
Assert.That(cache.Current.Key, Is.EqualTo(context1.Key));
34+
}
35+
36+
Assert.That(cache.Current.Key, Is.EqualTo(context2.Key));
37+
}
38+
}
39+
}

Shuttle.Core.Data/.build/package.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency id="Shuttle.Core.Logging" version="{Shuttle.Core.Logging-version}" />
2929
</group>
3030

31-
<group targetFramework="netcoreapp2.0">
31+
<group targetFramework="netcoreapp2.1">
3232
<dependency id="System.Configuration.ConfigurationManager" version="{System.Configuration.ConfigurationManager-version}" />
3333
<dependency id="Shuttle.Core.Configuration" version="{Shuttle.Core.Configuration-version}" />
3434
<dependency id="Shuttle.Core.Container" version="{Shuttle.Core.Container-version}" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Shuttle.Core.Contract;
3+
4+
namespace Shuttle.Core.Data
5+
{
6+
public class ActiveDatabaseContext : IDisposable
7+
{
8+
private readonly IDatabaseContextCache _databaseContextCache;
9+
private readonly IDatabaseContext _current;
10+
11+
public ActiveDatabaseContext(IDatabaseContextCache databaseContextCache, IDatabaseContext current)
12+
{
13+
Guard.AgainstNull(databaseContextCache, nameof(databaseContextCache));
14+
15+
_databaseContextCache = databaseContextCache;
16+
_current = current;
17+
}
18+
19+
public void Dispose()
20+
{
21+
if (_current == null)
22+
{
23+
return;
24+
}
25+
26+
_databaseContextCache.Use(_current);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)