-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (76 loc) · 3.19 KB
/
Copy pathProgram.cs
File metadata and controls
94 lines (76 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Wolverine.FluentValidation;
using Marten;
using Wolverine;
using Wolverine.Http;
using Wolverine.Http.FluentValidation;
using Wolverine.Marten;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddWolverineHttp();
builder.Services.AddMarten(opts =>
{
var connectionString = builder.Configuration.GetConnectionString("Marten")
?? "Host=localhost;Port=5433;Database=ecommerce;Username=postgres;Password=postgres";
opts.Connection(connectionString);
// Each module gets its own schema — clean separation within one database
opts.Schema.For<Catalog.Product>().DatabaseSchemaName("catalog");
opts.Schema.For<Basket.ShoppingCart>().DatabaseSchemaName("basket").Identity(x => x.Id);
opts.Schema.For<Ordering.Order>().DatabaseSchemaName("ordering");
opts.Schema.For<Ordering.Order>().Index(x => x.CustomerId);
opts.Schema.For<Ordering.Order>().Index(x => x.OrderName);
opts.Schema.For<Discount.Coupon>().DatabaseSchemaName("discount");
opts.Schema.For<Discount.Coupon>().Index(x => x.ProductName);
})
.IntegrateWithWolverine()
.UseLightweightSessions();
builder.Host.UseWolverine(opts =>
{
opts.Discovery.IncludeAssembly(typeof(Program).Assembly);
opts.Policies.AutoApplyTransactions();
opts.UseFluentValidation();
opts.ServiceName = "EcommerceMonolith";
// Inter-module messaging uses durable local queues —
// messages survive process restarts via the Marten outbox
opts.LocalQueue("basket-checkout").UseDurableInbox();
opts.LocalQueue("order-notifications").UseDurableInbox();
// Route BasketCheckoutEvent to the durable local queue
opts.Publish(x =>
{
x.Message<Shared.BasketCheckoutEvent>();
x.ToLocalQueue("basket-checkout");
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapWolverineEndpoints(opts =>
{
opts.UseFluentValidationProblemDetailMiddleware();
});
await SeedData(app);
await app.RunAsync();
static async Task SeedData(WebApplication app)
{
using var scope = app.Services.CreateScope();
var session = scope.ServiceProvider.GetRequiredService<IDocumentSession>();
if (!await session.Query<Catalog.Product>().AnyAsync())
{
session.Store(
new Catalog.Product { Name = "IPhone X", Category = ["Smart Phone"], Description = "Apple smartphone", ImageFile = "product-1.png", Price = 950.00m },
new Catalog.Product { Name = "Samsung 10", Category = ["Smart Phone"], Description = "Samsung smartphone", ImageFile = "product-2.png", Price = 840.00m },
new Catalog.Product { Name = "Huawei Plus", Category = ["White Appliances"], Description = "Huawei phone", ImageFile = "product-3.png", Price = 650.00m }
);
}
if (!await session.Query<Discount.Coupon>().AnyAsync())
{
session.Store(
new Discount.Coupon { ProductName = "IPhone X", Description = "IPhone Discount", Amount = 150 },
new Discount.Coupon { ProductName = "Samsung 10", Description = "Samsung Discount", Amount = 100 }
);
}
await session.SaveChangesAsync();
}