-
-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (66 loc) · 2.31 KB
/
Copy pathProgram.cs
File metadata and controls
89 lines (66 loc) · 2.31 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
using AspNetCoreWithMarten;
using JasperFx;
using Marten;
using Marten.Services.Json;
using Microsoft.AspNetCore.Mvc;
using JasperFx;
using Weasel.Core;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Host.ApplyJasperFxExtensions();
#region sample_startupconfigureservices
// This is the absolute, simplest way to integrate Marten into your
// .NET application with Marten's default configuration
builder.Services.AddMarten(options =>
{
// Establish the connection string to your Marten database
options.Connection(builder.Configuration.GetConnectionString("Marten")!);
// If you want the Marten controlled PostgreSQL objects
// in a different schema other than "public"
options.DatabaseSchemaName = "other";
// There are of course, plenty of other options...
})
// This is recommended in new development projects
.UseLightweightSessions()
// If you're using Aspire, use this option *instead* of specifying a connection
// string to Marten
.UseNpgsqlDataSource();
#endregion
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
#region sample_userendpoints
// You can inject the IDocumentStore and open sessions yourself
app.MapPost("/user",
async (CreateUserRequest create,
// Inject a session for querying, loading, and updating documents
[FromServices] IDocumentSession session) =>
{
var user = new User {
FirstName = create.FirstName,
LastName = create.LastName,
Internal = create.Internal
};
session.Store(user);
// Commit all outstanding changes in one
// database transaction
await session.SaveChangesAsync();
});
app.MapGet("/users",
async (bool internalOnly, [FromServices] IDocumentSession session, CancellationToken ct) =>
{
return await session.Query<User>()
.Where(x=> x.Internal == internalOnly)
.ToListAsync(ct);
});
// OR use the lightweight IQuerySession if all you're doing is running queries
app.MapGet("/user/{id:guid}",
async (Guid id, [FromServices] IQuerySession session, CancellationToken ct) =>
{
return await session.LoadAsync<User>(id, ct);
});
#endregion
return await app.RunJasperFxCommands(args);
record CreateUserRequest(string FirstName, string LastName, bool Internal);