Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKneale committed Sep 20, 2024
1 parent e63e22c commit 81e1bf0
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 208 deletions.
28 changes: 28 additions & 0 deletions templates/Host/src/Common/Behaviours/TxBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Common.Infrastructure;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Common.Behaviours;

public class TxBehaviour<TRequest, TResponse>(IDbConnectionFactory connections, ILogger<TRequest> log) : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
log.LogInformation("Begin transaction");
var connection = await connections.OpenAsync();
var tx = await connection.BeginTransactionAsync(cancellationToken);
try
{
var result = await next();
await tx.CommitAsync(cancellationToken);
return result;
}
catch (Exception e)
{
log.LogError(e, "Error executing transaction");
await tx.RollbackAsync(cancellationToken);
throw;
}
}
}
12 changes: 11 additions & 1 deletion templates/Host/src/Common/Infrastructure/DbConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Common.Infrastructure;

public class DbConnectionFactory(string connectionString) : IDbConnectionFactory
public class DbConnectionFactory(string connectionString) : IDbConnectionFactory, IDisposable, IAsyncDisposable
{
private NpgsqlConnection? _connection;

Expand All @@ -14,4 +14,14 @@ public async Task<NpgsqlConnection> OpenAsync()
await _connection.OpenAsync();
return _connection;
}

public void Dispose()
{
_connection?.Dispose();
}

public async ValueTask DisposeAsync()
{
if (_connection != null) await _connection.DisposeAsync();
}
}
1 change: 1 addition & 0 deletions templates/Host/src/Common/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static IServiceCollection AddCommon(this IServiceCollection services)
{
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(TxBehaviour<,>));

services.AddScoped<IntegrationEventRepository>();
return services;
Expand Down
163 changes: 0 additions & 163 deletions templates/Host/src/Host/Infrastructure/Modules/ModuleRegistration.cs

This file was deleted.

10 changes: 10 additions & 0 deletions templates/Host/src/Host/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
@page
@model Host.Pages.Index

<div class="row">
<div class="col-12">
<h1>Modular Monolith</h1>
<p>Modular Monolith is a project that demonstrates how to build a modular monolith application using ASP.NET Core.</p>
<a href="ModularMonolithModule">Module</a>
<br>
<a href="Swagger">Swagger</a>
</div>
</div>
7 changes: 7 additions & 0 deletions templates/Host/src/Host/Pages/Shared/_Page.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@{
Layout = "_Layout";
}

<div class="container">
@RenderBody()
</div>
2 changes: 1 addition & 1 deletion templates/Host/src/Host/Pages/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@{
Layout = "_Layout";
Layout = "_Page";
}
26 changes: 16 additions & 10 deletions templates/Host/src/Host/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
using Common;
using Host.Infrastructure.Integration;
using Host.Infrastructure.Modules;
using ModularMonolithModule;
using ModularMonolithModule.Api;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddLogging(x => x.AddSimpleConsole(opt => opt.SingleLine = true));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var module = typeof(ModularMonolithModule.ModularMonolithModule).Assembly;

// modules
builder.Services.AddModules();
builder.Services.AddSingleton<IModularMonolithModule, ModularMonolithModule.ModularMonolithModule>();
builder.Services.AddSingleton<IModule, ModularMonolithModule.ModularMonolithModule>();
builder.Services.AddSingleton<ModularMonolithModuleStartup>();

// ui
builder.Services
.AddRazorPages()
.AddRazorRuntimeCompilation()
.AddWebModules();

// web
builder.Services
.AddWebModulesFiles();
.AddApplicationPart(module)
.AddRazorRuntimeCompilation(c => c.FileProviders.Add(new EmbeddedFileProvider(module)));

// queue
builder.Services
Expand All @@ -37,10 +39,14 @@
app.UseRouting();
// app.UseAuthentication();
// app.UseAuthorization();
app.UseApiModules();
app.UseModuleEndpoints();

app.MapGet("/meta/name", () => Assembly.GetExecutingAssembly().GetName());
app.MapGet("/health/alive", () => "alive");
app.MapGet("/health/ready", () => "ready");
app.MapRazorPages();
app.StartModules();

// module endpoints
app.Services.GetRequiredService<ModularMonolithModuleStartup>().Startup();

app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public static class ModularMonolithModuleEndpoints
{
private static readonly ModularMonolithModule Module = new();

public static void RegisterEndpoints(this WebApplication app)
public static void UseModuleEndpoints(this WebApplication app)
{
var root = ModularMonolithModuleApiAssemblyInfo.Assembly.GetName().Name;
var path = $"/{root}/widgets";
app.MapGet(path, (CancellationToken token) =>
app.MapGet(path, async (CancellationToken token) =>
{
var results = Module.SendQuery(new ListWidgets.Query(),token);
var results = await Module.SendQuery(new ListWidgets.Query(),token);
return Results.Ok(results);
})
.WithName($"{root} - GetWidgets ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
@page
@model ModularMonolithModule.Create
<form method="post">
<div class="mb-3">
<label class="form-label">Name</label>
<input type="text" name="name" class="form-control"/>
</div>
<div class="mb-3">
<label class="form-label">Price</label>
<input type="text" name="price" class="form-control"/>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using ModularMonolithModule.Application.Commands;
using Microsoft.AspNetCore.Mvc;
using ModularMonolithModule.Application.Commands;

namespace ModularMonolithModule.Web.Pages.ModularMonolithModule;

public class Create(IModularMonolithModule module) : PageModel
{
public async Task OnPostAsync(string name, decimal price, CancellationToken cancellationToken)
public async Task<RedirectToPageResult> OnPostAsync(string name, decimal price, CancellationToken cancellationToken)
{
var command = new CreateWidget.Command(Guid.NewGuid(), name, price);
await module.SendCommand(command, cancellationToken);
return RedirectToPage(nameof(Index));
}
}
Loading

0 comments on commit 81e1bf0

Please sign in to comment.