Skip to content

Commit

Permalink
Add John.EnumToString、John.Api.MinimalApi
Browse files Browse the repository at this point in the history
  • Loading branch information
john72831 committed Mar 11, 2023
1 parent 6be70db commit eae65cb
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 0 deletions.
10 changes: 10 additions & 0 deletions John.Api.MinimalApi/John.Api.MinimalApi.csproj
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>
94 changes: 94 additions & 0 deletions John.Api.MinimalApi/Program.cs
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);
}
}
41 changes: 41 additions & 0 deletions John.Api.MinimalApi/Properties/launchSettings.json
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"
}
}
}
}
8 changes: 8 additions & 0 deletions John.Api.MinimalApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions John.Api.MinimalApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
14 changes: 14 additions & 0 deletions John.EnumToString/John.EnumToString.csproj
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>
52 changes: 52 additions & 0 deletions John.EnumToString/Program.cs
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
}

0 comments on commit eae65cb

Please sign in to comment.