Skip to content

Commit 0a0b361

Browse files
authored
Merge pull request #55 from jkone27/add-csharp-sample
add csharp sample with tests from F#
2 parents 31c3afd + 24bc54c commit 0a0b361

File tree

14 files changed

+294
-0
lines changed

14 files changed

+294
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# sample usage
2+
# https://github.com/typicode/json-server
3+
# http://localhost:3000/persons?age=25
4+
# http://localhost:3000/persons/1
5+
6+
FROM node:alpine3.19
7+
RUN mkdir -p /app
8+
WORKDIR /app
9+
RUN npm install -g json-server
10+
CMD ["json-server", "--watch", "db.json", "--host", "0.0.0.0"]

samples/web-csharp/Web.CSharp.sln

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web.CSharp", "src\Web.CSharp.csproj", "{69EC9787-159F-43CC-A179-AFF77D4E85A5}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CED50CB1-C8A7-45D2-B9AA-A54CEEF16418}"
9+
EndProject
10+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Web.CSharp.Test", "test\Web.CSharp.Test.fsproj", "{7C8E4848-201B-456F-994E-3701B4E0DFCD}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{69EC9787-159F-43CC-A179-AFF77D4E85A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{69EC9787-159F-43CC-A179-AFF77D4E85A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{69EC9787-159F-43CC-A179-AFF77D4E85A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{69EC9787-159F-43CC-A179-AFF77D4E85A5}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{7C8E4848-201B-456F-994E-3701B4E0DFCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{7C8E4848-201B-456F-994E-3701B4E0DFCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{7C8E4848-201B-456F-994E-3701B4E0DFCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{7C8E4848-201B-456F-994E-3701B4E0DFCD}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(NestedProjects) = preSolution
31+
{7C8E4848-201B-456F-994E-3701B4E0DFCD} = {CED50CB1-C8A7-45D2-B9AA-A54CEEF16418}
32+
EndGlobalSection
33+
EndGlobal
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3.8'
2+
3+
services:
4+
mock-server:
5+
container_name: json-server
6+
build:
7+
context: .
8+
dockerfile: JsonServerDocker
9+
ports:
10+
- "3000:3000"
11+
volumes:
12+
- ./mock-data:/app
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"persons": [
3+
{
4+
"id": "1",
5+
"name": "John",
6+
"age": 30
7+
},
8+
{
9+
"id": "2",
10+
"name": "Jane",
11+
"age": 25
12+
}
13+
]
14+
}

samples/web-csharp/src/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.Services.AddHttpClient<PersonClient>();
6+
builder.Services.AddTransient<PersonRepository>();
7+
var app = builder.Build();
8+
9+
app.MapGet("/", () => "Hello World!");
10+
11+
app.MapGet("/john",
12+
async ctx => {
13+
var repo = ctx.RequestServices.GetRequiredService<PersonRepository>();
14+
15+
var john = await repo.GetPerson("John");
16+
17+
ctx.Response.StatusCode = 200;
18+
await ctx.Response.WriteAsJsonAsync(john);
19+
});
20+
21+
app.Run();
22+
23+
// NOTE: add this to be able to test with WebApplicationFactory
24+
public partial class Program { }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:5078",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": true,
17+
"applicationUrl": "https://localhost:7024;http://localhost:5078",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}

samples/web-csharp/src/Services.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient
2+
3+
// DTO
4+
public record Person(string Name, int Age);
5+
6+
7+
// http typed client
8+
public class PersonClient
9+
{
10+
private HttpClient httpClient;
11+
private readonly Random rnd = new Random();
12+
13+
public PersonClient(HttpClient httpClient)
14+
{
15+
this.httpClient = httpClient;
16+
}
17+
18+
public async Task<Person?> GetByName(string name)
19+
{
20+
var result = await httpClient.GetFromJsonAsync<Person[]>($"persons?name={name}");
21+
22+
return result?.FirstOrDefault();
23+
}
24+
25+
}
26+
27+
// service using this client
28+
public class PersonRepository
29+
{
30+
private readonly PersonClient personClient;
31+
public PersonRepository(PersonClient personClient)
32+
{
33+
this.personClient = personClient;
34+
}
35+
36+
public Task<Person?> GetPerson(string name) => personClient.GetByName(name);
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)