Skip to content

Commit

Permalink
Swapped out StaticDataStoreRegistry for Scoped version. That will cau…
Browse files Browse the repository at this point in the history
…se fewer issues with threadlocking and background jobs.
  • Loading branch information
jasonmwebb-lv committed May 23, 2024
1 parent f714ecb commit 4390a44
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions Src/RCommon.Persistence/Crud/LinqRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public string DataStoreName
var dataStore = this.DataStoreRegistry.GetDataStore(_dataStoreName);

// Enlist Data Stores that are participating in transactions
if (this.UnitOfWorkManager.CurrentUnitOfWork != null)
if (this.UnitOfWorkManager.IsUnitOfWorkActive)
{
this._dataStoreEnlistmentProvider.EnlistDataStore(this.UnitOfWorkManager.CurrentUnitOfWork.TransactionId, dataStore);
this._dataStoreEnlistmentProvider.EnlistDataStore(this.UnitOfWorkManager.CurrentUnitOfWorkTransactionId, dataStore);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Src/RCommon.Persistence/Crud/SqlRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public string DataStoreName
var dataStore = this.DataStoreRegistry.GetDataStore(_dataStoreName);

// Enlist Data Stores that are participating in transactions
if (this.UnitOfWorkManager.CurrentUnitOfWork != null)
if (this.UnitOfWorkManager.IsUnitOfWorkActive)
{
this._dataStoreEnlistmentProvider.EnlistDataStore(this.UnitOfWorkManager.CurrentUnitOfWork.TransactionId, dataStore);
this._dataStoreEnlistmentProvider.EnlistDataStore(this.UnitOfWorkManager.CurrentUnitOfWorkTransactionId, dataStore);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions Src/RCommon.Persistence/IScopedDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Concurrent;

namespace RCommon.Persistence
{
public interface IScopedDataStore
{
ConcurrentDictionary<string, Type> DataStores { get; set; }
}
}
9 changes: 4 additions & 5 deletions Src/RCommon.Persistence/PersistenceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
where TUnitOfWork : IUnitOfWorkBuilder
{
// Data Store Management
StaticDataStore.DataStores = (StaticDataStore.DataStores == null ? new System.Collections.Concurrent.ConcurrentDictionary<string, Type>()
: StaticDataStore.DataStores);
builder.Services.AddScoped<IDataStoreRegistry, StaticDataStoreRegistry>();
builder.Services.AddScoped<IScopedDataStore, ScopedDataStore>();
builder.Services.AddScoped<IDataStoreRegistry, ScopedDataStoreRegistry>();

// Object Access and Unit of Work Configurations
// Wire up the "out of the box" events/event handlers used in persistence. These are not transactional
Expand All @@ -60,7 +59,7 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
objectAccessActions(dataConfiguration);
var unitOfWorkConfiguration = (TUnitOfWork)Activator.CreateInstance(typeof(TUnitOfWork), new object[] { builder.Services });
unitOfWorkActions(unitOfWorkConfiguration);
builder = WithChangeTracking(builder);
builder = WithEventTracking(builder);
return builder;
}

Expand All @@ -71,7 +70,7 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
/// </summary>
/// <param name="builder">Instance of <see cref="IRCommonBuilder"/>passed in.</param>
/// <returns>Updated instance of <see cref="IRCommonBuilder"/>RCommon Configuration</returns>
private static IRCommonBuilder WithChangeTracking(this IRCommonBuilder builder)
private static IRCommonBuilder WithEventTracking(this IRCommonBuilder builder)
{
builder.Services.AddScoped<IEventRouter, InMemoryTransactionalEventRouter>();
builder.Services.AddScoped<IEntityEventTracker, InMemoryEntityEventTracker>();
Expand Down
20 changes: 20 additions & 0 deletions Src/RCommon.Persistence/ScopedDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.Persistence
{
public class ScopedDataStore : IScopedDataStore
{

public ScopedDataStore()
{
DataStores = new ConcurrentDictionary<string, Type>();
}

public ConcurrentDictionary<string, Type> DataStores { get; set; }
}
}
52 changes: 52 additions & 0 deletions Src/RCommon.Persistence/ScopedDataStoreRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.Persistence
{
public class ScopedDataStoreRegistry : IDataStoreRegistry
{
private readonly IServiceProvider _serviceProvider;
private readonly IScopedDataStore _scopedDataStore;

public ScopedDataStoreRegistry(IServiceProvider serviceProvider, IScopedDataStore scopedDataStore)
{
_serviceProvider = serviceProvider;
_scopedDataStore = scopedDataStore;
}

public TDataStore GetDataStore<TDataStore>(string dataStoreName) where TDataStore : IDataStore
{
var type = _scopedDataStore.DataStores.Where(x => x.Key == dataStoreName).FirstOrDefault().Value;
Guard.Against<DataStoreNotFoundException>(type == null,
this.GetGenericTypeName() + " could not find a DataStore with the key of: " + dataStoreName);
return (TDataStore)this._serviceProvider.GetService(type);
}

public IDataStore GetDataStore(string dataStoreName)
{
var type = _scopedDataStore.DataStores.Where(x => x.Key == dataStoreName).FirstOrDefault().Value;
Guard.Against<DataStoreNotFoundException>(type == null,
this.GetGenericTypeName() + " could not find a DataStore with the key of: " + dataStoreName);
return (IDataStore)this._serviceProvider.GetService(type);
}

public void RegisterDataStore<TDataStore>(TDataStore dataStore, string dataStoreName) where TDataStore : IDataStore
{
if (!_scopedDataStore.DataStores.TryAdd(dataStoreName, typeof(TDataStore)))
{
throw new UnsupportedDataStoreException($"The ScopedDataStore refused to add the new DataStore name: {dataStoreName} of type: {dataStore.GetType().AssemblyQualifiedName}");
}
}

public void RemoveRegisteredDataStore(string dataStoreName)
{
if (!_scopedDataStore.DataStores.TryRemove(dataStoreName, out _))
{
throw new UnsupportedDataStoreException($"The ScopedDataStore refused to remove the DataStore name: {dataStoreName}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/RCommon.Emailing.Tests/RCommon.Emailing.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
</PackageReference>
<PackageReference Include="Dapper.FluentMap.Dommel" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="ReportGenerator" Version="5.2.5" />
<PackageReference Include="ReportGenerator" Version="5.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Src\RCommon.Linq2Db\RCommon.Linq2Db.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/RCommon.Security.Tests/RCommon.Security.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion Tests/RCommon.TestBase.Data/RCommon.TestBase.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Tests/RCommon.TestBase/RCommon.TestBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/RCommon.Tests/RCommon.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 4390a44

Please sign in to comment.