-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (98 loc) · 3.65 KB
/
Copy pathProgram.cs
File metadata and controls
123 lines (98 loc) · 3.65 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using System;
var builder = WebApplication.CreateBuilder(args);
// Register services with different lifetimes
// Uncomment one at a time to test each scope
// builder.Services.AddSingleton<IMyService, MyService>(); // Singleton
// builder.Services.AddScoped<IMyService, MyService>(); // Scoped
builder.Services.AddTransient<IMyService, MyService>(); // Transient
builder.Services.AddSingleton<ProductStore>();
var app = builder.Build();
// Middleware to demonstrate lifecycle in multiple parts of the pipeline
app.Use(async (context, next) =>
{
var myService = context.RequestServices.GetRequiredService<IMyService>();
myService.LogCreation("First Middleware");
await next();
});
app.Use(async (context, next) =>
{
var myService = context.RequestServices.GetRequiredService<IMyService>();
myService.LogCreation("Second Middleware");
await next();
});
// Final endpoint to demonstrate service lifecycle in the request
app.MapGet("/", (IMyService myService) =>
{
myService.LogCreation("Root");
return Results.Ok("Check the console for service creation logs.");
});
app.MapGet("/products", (ProductStore store) => Results.Ok(store.GetAll()));
app.MapGet("/products/{id:int}", (int id, ProductStore store) =>
{
var product = store.GetById(id);
return product is null ? Results.NotFound() : Results.Ok(product);
});
app.MapPost("/products", (CreateProductRequest request, ProductStore store) =>
{
if (string.IsNullOrWhiteSpace(request.Name))
{
return Results.BadRequest(new { message = "Product name is required." });
}
var product = store.Create(request);
return Results.Created($"/products/{product.Id}", product);
});
app.MapPut("/products/{id:int}", (int id, UpdateProductRequest request, ProductStore store) =>
{
if (string.IsNullOrWhiteSpace(request.Name))
{
return Results.BadRequest(new { message = "Product name is required." });
}
var product = store.Update(id, request);
return product is null ? Results.NotFound() : Results.Ok(product);
});
app.MapDelete("/products/{id:int}", (int id, ProductStore store) =>
store.Delete(id) ? Results.NoContent() : Results.NotFound());
app.Run();
public record Product(int Id, string Name, string? Description);
public record CreateProductRequest(string Name, string? Description);
public record UpdateProductRequest(string Name, string? Description);
public sealed class ProductStore
{
private readonly List<Product> _products =
[
new Product(1, "Keyboard", "Mechanical keyboard"),
new Product(2, "Mouse", "Wireless mouse")
];
private int _nextId = 3;
public IReadOnlyList<Product> GetAll() => _products;
public Product? GetById(int id) => _products.FirstOrDefault(product => product.Id == id);
public Product Create(CreateProductRequest request)
{
var product = new Product(_nextId++, request.Name, request.Description);
_products.Add(product);
return product;
}
public Product? Update(int id, UpdateProductRequest request)
{
var existingProduct = GetById(id);
if (existingProduct is null)
{
return null;
}
var updatedProduct = existingProduct with
{
Name = request.Name,
Description = request.Description
};
_products[_products.IndexOf(existingProduct)] = updatedProduct;
return updatedProduct;
}
public bool Delete(int id)
{
var product = GetById(id);
return product is not null && _products.Remove(product);
}
}