Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectionString instead of connection for TransactionScope #12

Open
wants to merge 1 commit into
base: TransactionScope-Connection
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shop.Communication.DataAccess.Interfaces;
using Shop.Framework.UseCases.Interfaces.Services;
using Shop.Utils.Modules;

namespace Shop.Communication.DataAccess.MsSql
Expand All @@ -12,8 +12,7 @@ public override void Load(IServiceCollection services)
{
services.AddDbContext<ICommunicationDbContext, CommunicationDbContext>((sp, bld) =>
{
var factory = sp.GetRequiredService<IConnectionFactory>();
bld.UseSqlServer(factory.GetConnection());
bld.UseSqlServer(Configuration.GetConnectionString("MsSqlConnection"));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shop.Framework.UseCases.Implementation.Services;
using Shop.Framework.UseCases.Interfaces.Services;
Expand All @@ -12,7 +11,6 @@ public class FrameworkModule : Module
public override void Load(IServiceCollection services)
{
services.AddScoped<ICurrentUserService, CurrentUserService>();
services.AddScoped<IConnectionFactory, ConnectionFactory>(factory => new ConnectionFactory(Configuration.GetConnectionString("MsSqlConnection")));

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shop.Framework.UseCases.Interfaces.Services;
using Shop.Order.DataAccess.Interfaces;
using Shop.Utils.Modules;

Expand All @@ -12,8 +12,7 @@ public override void Load(IServiceCollection services)
{
services.AddDbContext<IOrderDbContext, OrderDbContext>((sp, bld) =>
{
var factory = sp.GetRequiredService<IConnectionFactory>();
bld.UseSqlServer(factory.GetConnection());
bld.UseSqlServer(Configuration.GetConnectionString("MsSqlConnection"));
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions ModularMonolith/Shop.Tests.Unit/WorkflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public async Task Should_Not_Create_Order_And_Email_On_Error()
//services.Decorate<ICommunicationDbContext, TestCommunicationDbContext>();
services.AddDbContext<CommunicationDbContext>((sp, bld) =>
{
var factory = sp.GetRequiredService<IConnectionFactory>();
bld.UseSqlServer(factory.GetConnection());
bld.UseSqlServer(configuration.GetConnectionString("MsSqlConnection"));
});
services.AddScoped<ICommunicationDbContext, TestCommunicationDbContext>();

Expand Down
15 changes: 0 additions & 15 deletions ModularMonolith/Shop.Web/Utils/TransactionScopePipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@
using System.Threading.Tasks;
using System.Transactions;
using MediatR;
using Shop.Framework.UseCases.Interfaces.Services;
using Shop.Framework.UseCases.Interfaces.Transactions;

namespace Shop.Web.Utils
{
public class TransactionScopePipelineBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>, ITransactionalRequest
{
private readonly IConnectionFactory _connectionFactory;

public TransactionScopePipelineBehavior(IConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
if (_connectionFactory.IsConnectionOpened)
{
return await next();
}

using var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);

await using var connection = _connectionFactory.GetConnection();

var result = await next();

scope.Complete();
Expand Down