-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add John.EnumToString、John.Api.MinimalApi
- Loading branch information
Showing
7 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>John.Api_MinimalApi</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services.AddSingleton<CustomerRepository>(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapGet("/customers", ([FromServices] CustomerRepository repo) => | ||
{ | ||
return repo.GetAll(); | ||
}); | ||
|
||
app.MapGet("/customers/{id}", ([FromServices] CustomerRepository repo, Guid id) => | ||
{ | ||
var customer = repo.GetById(id); | ||
|
||
return customer is not null ? Results.Ok(customer) : Results.NotFound(); | ||
}); | ||
|
||
app.MapPost("/customers", ([FromServices] CustomerRepository repo, Customer customer) => | ||
{ | ||
repo.Create(customer); | ||
|
||
return Results.Created($"/customers/{customer.Id}", customer); | ||
}); | ||
|
||
app.MapPost("/customers/{id}", ([FromServices] CustomerRepository repo, Guid id, Customer updatedCustomer) => | ||
{ | ||
var customer = repo.GetById(id); | ||
|
||
if (customer is null) | ||
{ | ||
return Results.NotFound(); | ||
} | ||
|
||
repo.Update(updatedCustomer); | ||
|
||
return Results.Ok(updatedCustomer); | ||
}); | ||
|
||
app.MapPost("/customers/{id}", ([FromServices] CustomerRepository repo, Guid id) => | ||
{ | ||
repo.Delete(id); | ||
|
||
return Results.Ok(); | ||
}); | ||
|
||
app.Run(); | ||
|
||
record Customer(Guid Id, string FullName); | ||
|
||
class CustomerRepository | ||
{ | ||
private readonly Dictionary<Guid, Customer> _customers = new(); | ||
|
||
public void Create(Customer customer) | ||
{ | ||
if (customer == null) | ||
{ | ||
return; | ||
} | ||
|
||
_customers[customer.Id] = customer; | ||
} | ||
|
||
public Customer GetById(Guid Id) | ||
{ | ||
return _customers[Id]; | ||
} | ||
|
||
public List<Customer?> GetAll() | ||
{ | ||
return _customers.Values.ToList(); | ||
} | ||
|
||
public void Update(Customer customer) | ||
{ | ||
var existingCustomer = GetById(customer.Id); | ||
|
||
if (existingCustomer != null) | ||
{ | ||
return; | ||
} | ||
|
||
_customers[customer.Id] = customer; | ||
} | ||
|
||
public void Delete(Guid Id) | ||
{ | ||
_customers.Remove(Id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:39142", | ||
"sslPort": 44364 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"applicationUrl": "https://localhost:7118;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "weatherforecast", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
| ||
|
||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Running; | ||
using System.Diagnostics; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
BenchmarkRunner.Run<Benchy>(); | ||
|
||
[MemoryDiagnoser(false)] | ||
public class Benchy | ||
{ | ||
[Benchmark] | ||
public string NativeToString() | ||
{ | ||
return HumanStates.Dead.ToString(); ; | ||
} | ||
|
||
[Benchmark] | ||
public string FastToString() | ||
{ | ||
return FastToString(HumanStates.Dead); | ||
} | ||
|
||
private static string FastToString(HumanStates states) | ||
{ | ||
switch (states) | ||
{ | ||
case HumanStates.Idle: | ||
return nameof(HumanStates.Idle); | ||
case HumanStates.Working: | ||
return nameof(HumanStates.Working); | ||
case HumanStates.Sleeping: | ||
return nameof(HumanStates.Sleeping); | ||
case HumanStates.Eating: | ||
return nameof(HumanStates.Eating); | ||
case HumanStates.Dead: | ||
return nameof(HumanStates.Dead); | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(states), states, null); | ||
} | ||
} | ||
} | ||
|
||
enum HumanStates | ||
{ | ||
Idle, | ||
Working, | ||
Sleeping, | ||
Eating, | ||
Dead | ||
} |