Skip to content

Commit

Permalink
IServerExecutionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Feb 29, 2024
1 parent 3969b81 commit b5844ed
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public async Task<ActionResult> RedirectToStripePremiumSubscription([FromRoute]
var createStripeCheckoutSession = new CreateStripeCheckoutSession
{
SubscriptionPlanType = subscriptionPlanType,
UserId = executionContext.UserId,
TenantId = executionContext.TenantId,
RedirectBaseUrl = executionContext.BaseURI.AbsoluteUri
UserId = ExecutionContext.UserId,
TenantId = ExecutionContext.TenantId,
RedirectBaseUrl = ExecutionContext.BaseURI.AbsoluteUri
};
var checkoutSession = await commandDispatcher.DispatchAsync<CreateStripeCheckoutSession, Stripe.Checkout.Session>(createStripeCheckoutSession);

Expand All @@ -38,8 +38,8 @@ public async Task<ActionResult> Create()
{
var createBillingPortalSession = new CreateStripeBillingPortalSession
{
UserId = executionContext.UserId,
RedirectBaseUrl = executionContext.BaseURI.AbsoluteUri,
UserId = ExecutionContext.UserId,
RedirectBaseUrl = ExecutionContext.BaseURI.AbsoluteUri,
};
var billingPortalSession = await commandDispatcher.DispatchAsync<CreateStripeBillingPortalSession, Stripe.BillingPortal.Session>(createBillingPortalSession);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TenantsController(SignInManager<ApplicationUser> signInManager, IServiceP
[AuthorizeTenantAdmin]
public async Task<ActionResult<TenantDTO>> GetTenant()
{
var tenantId = executionContext.TenantId;
var tenantId = ExecutionContext.TenantId;
TenantDTO tenant = await queryDispatcher.DispatchAsync<GetTenantByID, TenantDTO>(new GetTenantByID { TenantId = tenantId });

return Ok(tenant);
Expand All @@ -40,7 +40,7 @@ public async Task<ActionResult<TenantDTO>> GetTenant()
[HttpGet("{tenantId}/details")]
public async Task<ActionResult<TenantDetailDTO>> GetTenantDetail()
{
var tenantId = executionContext.TenantId;
var tenantId = ExecutionContext.TenantId;
TenantDetailDTO tenantDetail = await queryDispatcher.DispatchAsync<GetTenantDetailsByID, TenantDetailDTO>(new GetTenantDetailsByID { TenantId = tenantId });

return Ok(tenantDetail);
Expand All @@ -50,7 +50,7 @@ public async Task<ActionResult<TenantDetailDTO>> GetTenantDetail()
[Authorize(AuthenticationSchemes = AuthConstant.ApplicationAuthenticationScheme)]
public async Task<ActionResult<IEnumerable<TenantDTO>>> GetAllTenantsWhereUserIsMember()
{
var userId = executionContext.UserId;
var userId = ExecutionContext.UserId;
List<TenantMembershipDTO> teamMemberships = await queryDispatcher.DispatchAsync<GetAllTenantMembershipsOfUser, List<TenantMembershipDTO>>(null);

return Ok(teamMemberships);
Expand All @@ -63,7 +63,7 @@ public async Task<ActionResult<TenantDTO>> CreateTenant(CreateTenantDTO createTe

var createTenant = new CreateTenantWithAdmin
{
AdminId = executionContext.UserId,
AdminId = ExecutionContext.UserId,
Name = createTenantDTO.Name
};
var createdTenant = await commandDispatcher.DispatchAsync<CreateTenantWithAdmin, TenantDTO>(null);
Expand All @@ -79,7 +79,7 @@ public async Task DeleteTenant(Guid id)
{
await commandDispatcher.DispatchAsync<DeleteTenant>(new DeleteTenant { });

var userId = executionContext.UserId;
var userId = ExecutionContext.UserId;
var user = await queryDispatcher.DispatchAsync<GetUserById, ApplicationUser>(new GetUserById { });

await signInManager.RefreshSignInAsync(user);
Expand All @@ -88,7 +88,7 @@ public async Task DeleteTenant(Guid id)
[HttpPost("memberships")]
public async Task<ActionResult> CreateTenantMembership(InviteUserToTenantDTO inviteUserToGroupDTO)
{
var userId = executionContext.UserId;
var userId = ExecutionContext.UserId;

await commandDispatcher.DispatchAsync<AddUserToTenant>(null);

Expand Down
8 changes: 5 additions & 3 deletions Source/Shared/Features/CQRS/Command/ICommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace Shared.Features.CQRS.Command
using Shared.Features.Server.ExecutionContext;

namespace Shared.Features.CQRS.Command
{
public interface ICommandHandler<in TCommand> where TCommand : ICommand
public interface ICommandHandler<in TCommand> : IInServerExecutionContextScope where TCommand : ICommand
{
Task HandleAsync(TCommand command, CancellationToken cancellationToken);
}
public interface ICommandHandler<in TCommand, TResult> where TCommand : ICommand<TResult>
public interface ICommandHandler<in TCommand, TResult> : IInServerExecutionContextScope where TCommand : ICommand<TResult>
{
Task<TResult> HandleAsync(TCommand command, CancellationToken cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Shared.Features.Domain;
using Shared.Features.Server.ExecutionContext;

namespace Shared.Features.CQRS.DomainEvent
{
public interface IDomainEventHandler<in TDomainEvent> where TDomainEvent : IDomainEvent
public interface IDomainEventHandler<in TDomainEvent> : IInServerExecutionContextScope where TDomainEvent : IDomainEvent
{
Task HandleAsync(TDomainEvent query, CancellationToken cancellation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Shared.Kernel.BuildingBlocks;
using Shared.Features.Server.ExecutionContext;
using Shared.Kernel.BuildingBlocks;

namespace Shared.Features.CQRS.IntegrationEvent
{
public interface IIntegrationEventHandler<in TIntegrationEvent> where TIntegrationEvent : IIntegrationEvent
public interface IIntegrationEventHandler<in TIntegrationEvent> : IInServerExecutionContextScope where TIntegrationEvent : IIntegrationEvent
{
Task HandleAsync(TIntegrationEvent integrationEvent, CancellationToken cancellation);
}
Expand Down
6 changes: 4 additions & 2 deletions Source/Shared/Features/CQRS/Query/IQueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Shared.Features.CQRS.Query
using Shared.Features.Server.ExecutionContext;

namespace Shared.Features.CQRS.Query
{
public interface IQueryHandler<in TQuery, TQueryResult> where TQuery : IQuery<TQueryResult>
public interface IQueryHandler<in TQuery, TQueryResult> : IInServerExecutionContextScope where TQuery : IQuery<TQueryResult>
{
Task<TQueryResult> HandleAsync(TQuery query, CancellationToken cancellation);
}
Expand Down
14 changes: 5 additions & 9 deletions Source/Shared/Features/Server/BaseController.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Shared.Features.CQRS.Command;
using Shared.Features.CQRS.Query;
using Shared.Features.Server.ExecutionContext;
using Shared.Kernel.BuildingBlocks;
using Shared.Kernel.BuildingBlocks.ModelValidation;

namespace Shared.Features.Server
{
public class BaseController : ControllerBase
public class BaseController : ControllerBase, IInServerExecutionContextScope
{
protected readonly ICommandDispatcher commandDispatcher;
protected readonly IQueryDispatcher queryDispatcher;
protected readonly IExecutionContext executionContext;
public IExecutionContext ExecutionContext { get; init; }

protected readonly IValidationService validationService;

public BaseController(IServiceProvider serviceProvider)
{
commandDispatcher = serviceProvider.GetRequiredService<ICommandDispatcher>();
queryDispatcher = serviceProvider.GetRequiredService<IQueryDispatcher>();
executionContext = serviceProvider.GetRequiredService<IExecutionContext>();
ExecutionContext = serviceProvider.GetRequiredService<IExecutionContext>();
validationService = serviceProvider.GetRequiredService<IValidationService>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Shared.Features.Server.ExecutionContext
{
public interface IInServerExecutionScope
public interface IInServerExecutionContextScope
{
public IExecutionContext ExecutionContext { get; init; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Shared.Features.CQRS.Command;
using Shared.Features.CQRS.DomainEvent;
using Shared.Features.CQRS.IntegrationEvent;
using Shared.Features.CQRS.Query;
using Shared.Kernel.BuildingBlocks;

namespace Shared.Features.Server.ExecutionContext
{
public interface IServerExecutionContext : IExecutionContext
{
public ICommandDispatcher CommandDispatcher { get; }
public IQueryDispatcher QueryDispatcher { get; }
public IIntegrationEventDispatcher IntegrationEventDispatcher { get; }
public IDomainEventDispatcher DomainEventDispatcher { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IServiceCollection AddServerExecutionContext(this IServiceCollecti
{
services.AddHttpContextAccessor();
services.AddScoped<ServiceExecutionContextMiddleware>();
services.AddScoped<IExecutionContext, ServerExecutionContext>(ServerExecutionContext.CreateInstance);
services.AddScoped<IServerExecutionContext, ServerExecutionContext>(ServerExecutionContext.CreateInstance);
return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shared.Features.CQRS.Command;
using Shared.Features.CQRS.DomainEvent;
using Shared.Features.CQRS.IntegrationEvent;
using Shared.Features.CQRS.Query;
using Shared.Kernel.BuildingBlocks.Auth;
using Shared.Kernel.BuildingBlocks;
using Shared.Kernel.Extensions.ClaimsPrincipal;

namespace Shared.Features.Server.ExecutionContext
{
public class ServerExecutionContext : IExecutionContext
public class ServerExecutionContext : IServerExecutionContext
{
private static ServerExecutionContext executionContext;
private ServerExecutionContext() { }
Expand All @@ -21,6 +24,10 @@ private ServerExecutionContext() { }
public TenantRole TenantRole { get; private set; }
public IHostEnvironment HostingEnvironment { get; set; }
public Uri BaseURI { get; private set; }
public ICommandDispatcher CommandDispatcher { get; private set; }
public IQueryDispatcher QueryDispatcher { get; private set; }
public IIntegrationEventDispatcher IntegrationEventDispatcher { get; private set; }
public IDomainEventDispatcher DomainEventDispatcher { get; private set; }

public static ServerExecutionContext CreateInstance(IServiceProvider serviceProvider)
{
Expand Down Expand Up @@ -52,6 +59,10 @@ public void InitializeInstance(HttpContext httpContext)
TenantId = httpContext.User.GetTenantId<Guid>();
TenantPlan = httpContext.User.GetTenantSubscriptionPlanType();
TenantRole = httpContext.User.GetTenantRole();
CommandDispatcher = httpContext.RequestServices.GetRequiredService<ICommandDispatcher>();
QueryDispatcher = httpContext.RequestServices.GetRequiredService<IQueryDispatcher>();
IntegrationEventDispatcher = httpContext.RequestServices.GetRequiredService<IIntegrationEventDispatcher>();
DomainEventDispatcher = httpContext.RequestServices.GetRequiredService<IDomainEventDispatcher>();
}
}
}

0 comments on commit b5844ed

Please sign in to comment.