Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 0 additions & 233 deletions AppForeach.Framework.sln

This file was deleted.

39 changes: 39 additions & 0 deletions AppForeach.Framework.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Solution>
<Folder Name="/samples/" />
<Folder Name="/samples/EscapeHit/">
<Project Path="samples/EscapeHit/EscapeHit.Invoice.Database.Design/EscapeHit.Invoice.Database.Design.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.Invoice.Database/EscapeHit.Invoice.Database.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.Invoice.Service/EscapeHit.Invoice.Service.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.Invoice.WebApi/EscapeHit.Invoice.WebApi.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.Invoice/EscapeHit.Invoice.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.Service/EscapeHit.Service.csproj" />
<Project Path="samples/EscapeHit/EscapeHit.WebApi/EscapeHit.WebApi.csproj" />
<Project Path="samples/EscapeHit/EscapeHit/EscapeHit.csproj" />
</Folder>
<Folder Name="/src/">
<Project Path="src/AppForeach.Framework.Autofac/AppForeach.Framework.Autofac.csproj" />
<Project Path="src/AppForeach.Framework.AutoMapper/AppForeach.Framework.AutoMapper.csproj" />
<Project Path="src/AppForeach.Framework.Castle.Windsor/AppForeach.Framework.Castle.Windsor.csproj" />
<Project Path="src/AppForeach.Framework.EntityFrameworkCore.Design/AppForeach.Framework.EntityFrameworkCore.Design.csproj" />
<Project Path="src/AppForeach.Framework.EntityFrameworkCore.PostgreSql.Design/AppForeach.Framework.EntityFrameworkCore.PostgreSql.Design.csproj" />
<Project Path="src/AppForeach.Framework.EntityFrameworkCore.PostgreSql/AppForeach.Framework.EntityFrameworkCore.PostgreSql.csproj" />
<Project Path="src/AppForeach.Framework.EntityFrameworkCore.SqlServer/AppForeach.Framework.EntityFrameworkCore.SqlServer.csproj" />
<Project Path="src/AppForeach.Framework.EntityFrameworkCore/AppForeach.Framework.EntityFrameworkCore.csproj" />
<Project Path="src/AppForeach.Framework.FluentValidation/AppForeach.Framework.FluentValidation.csproj" />
<Project Path="src/AppForeach.Framework.Hosting.Features.PostgreSql/AppForeach.Framework.Hosting.Features.PostgreSql.csproj" />
<Project Path="src/AppForeach.Framework.Hosting.Features.Serilog.Ecs/AppForeach.Framework.Hosting.Features.Serilog.Ecs.csproj" />
<Project Path="src/AppForeach.Framework.Hosting.Features.SqlServer/AppForeach.Framework.Hosting.Features.SqlServer.csproj" />
<Project Path="src/AppForeach.Framework.Hosting.Web/AppForeach.Framework.Hosting.Web.csproj" />
<Project Path="src/AppForeach.Framework.Hosting/AppForeach.Framework.Hosting.csproj" />
<Project Path="src/AppForeach.Framework.MassTransit/AppForeach.Framework.MassTransit.csproj" />
<Project Path="src/AppForeach.Framework.Microsoft.Extensions/AppForeach.Framework.Microsoft.Extensions.csproj" />
<Project Path="src/AppForeach.Framework.Serilog/AppForeach.Framework.Serilog.csproj" />
<Project Path="src/AppForeach.Framework/AppForeach.Framework.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/AppForeach.Framework.AutoMapper.Tests/AppForeach.Framework.AutoMapper.Tests.csproj" />
<Project Path="tests/AppForeach.Framework.EntityFrameworkCore.Tests/AppForeach.Framework.EntityFrameworkCore.Tests.csproj" />
<Project Path="tests/AppForeach.Framework.FluentValidation.Tests/AppForeach.Framework.FluentValidation.Tests.csproj" />
<Project Path="tests/AppForeach.Framework.Tests/AppForeach.Framework.Tests.csproj" />
</Folder>
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class AuditMiddleware : IOperationMiddleware
{
private readonly IOperationContext context;
private readonly ILoggingCorrelationProvider loggingCorrelationProvider;
private readonly IDbOptionsConfigurator dbOptionsConfigurator;
private readonly IDbContextActivator dbContextActivator;
private readonly IConnectionStringProvider connectionStringProvider;
private readonly IServiceProvider serviceProvider;

public AuditMiddleware(IOperationContext context, ILoggingCorrelationProvider loggingCorrelationProvider,
IDbOptionsConfigurator dbOptionsConfigurator, IConnectionStringProvider connectionStringProvider,
IDbContextActivator dbContextActivator, IConnectionStringProvider connectionStringProvider,
IServiceProvider serviceProvider)
{
this.context = context;
this.loggingCorrelationProvider = loggingCorrelationProvider;
this.dbOptionsConfigurator = dbOptionsConfigurator;
this.dbContextActivator = dbContextActivator;
this.connectionStringProvider = connectionStringProvider;
this.serviceProvider = serviceProvider;
}
Expand All @@ -35,9 +35,7 @@ public async Task ExecuteAsync(NextOperationDelegate next, CancellationToken can

if (auditEnabled)
{
var optionsBuilder = new DbContextOptionsBuilder<FrameworkDbContext>();
dbOptionsConfigurator.SetConnectionString(optionsBuilder, connectionStringProvider.ConnectionString);
using var db = (FrameworkDbContext)ActivatorUtilities.CreateInstance(serviceProvider, typeof(FrameworkDbContext), optionsBuilder.Options);
using var db = dbContextActivator.Activate<FrameworkDbContext>(DbContextOperationEnlistmentStrategy.Suppress);

var inputAudit = await AuditInput(db, cancellationToken);

Expand Down
38 changes: 29 additions & 9 deletions src/AppForeach.Framework.EntityFrameworkCore/DbContextActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public DbContextActivator(IOperationContext operationContext, IConnectionStringP
this.serviceProvider = serviceProvider;
}

public TDbContext Activate<TDbContext>() where TDbContext : DbContext
public TDbContext Activate<TDbContext>(DbContextOperationEnlistmentStrategy operationEnlistmentStrategy = DbContextOperationEnlistmentStrategy.Required) where TDbContext : DbContext
{
EnsureDbContextCanBeActivated();
bool? isCommand = GetIsCurrentOperationCommand(operationEnlistmentStrategy == DbContextOperationEnlistmentStrategy.Required);

TDbContext db;

if (operationContext.IsCommand)
if (isCommand == true && operationEnlistmentStrategy != DbContextOperationEnlistmentStrategy.Suppress)
{
var transationState = operationContext.State.Get<TransactionScopeState>();

Expand All @@ -46,26 +46,46 @@ public TDbContext Activate<TDbContext>() where TDbContext : DbContext

db = (TDbContext)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TDbContext), optionsBuilder.Options);

db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
db.SavingChanges += Db_SavingChanges;
if (isCommand == false)
{
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
db.SavingChanges += Db_SavingChanges;
}
}

return db;
}

private void EnsureDbContextCanBeActivated()
private bool? GetIsCurrentOperationCommand(bool isOperationMandatory)
{
var operationContextState = operationContext.State.Get<OperationContextState>();
if(!operationContextState.IsOperationInputSet)
{
throw new FrameworkException("DbContext cannot be activated outside of mediator execution context.");
if (isOperationMandatory)
{
throw new FrameworkException("DbContext cannot be activated outside of mediator execution context.");
}
else
{
return null;
}
}


var transationState = operationContext.State.Get<TransactionScopeState>();
if(!transationState.IsTransactionInitialized)
if (!transationState.IsTransactionInitialized)
{
throw new FrameworkException("DbContext cannot be activated outside of transaction middleware.");
if (isOperationMandatory)
{
throw new FrameworkException("DbContext cannot be activated outside of transaction middleware.");
}
else
{
return null;
}
}

return operationContext.IsCommand;
}

private void Db_SavingChanges(object sender, SavingChangesEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace AppForeach.Framework.EntityFrameworkCore
{
public enum DbContextOperationEnlistmentStrategy
{
Required,

Optional,

Suppress,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace AppForeach.Framework.EntityFrameworkCore
{
public interface IDbContextActivator
{
TDbContext Activate<TDbContext>() where TDbContext : DbContext;
TDbContext Activate<TDbContext>(DbContextOperationEnlistmentStrategy operationEnlistmentStrategy = DbContextOperationEnlistmentStrategy.Required) where TDbContext : DbContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AppForeach.Framework.EntityFrameworkCore\AppForeach.Framework.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Loading