Skip to content

Commit f64af79

Browse files
committed
Use new .NET features in net7.0 sample
(Minimal startup code; Minimal APIs for customers; file-scoped namespaces; nullable; implicit usings)
1 parent 1324a38 commit f64af79

24 files changed

+404
-578
lines changed

samples/net7.0/CustomersApi/Controllers/CustomersController.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

samples/net7.0/CustomersApi/CustomersApi.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net7.0</TargetFrameworks>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
57
</PropertyGroup>
68

79
<ItemGroup>
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
using Microsoft.EntityFrameworkCore;
22
using Shared;
33

4-
namespace Samples.CustomersApi.DataStore
4+
namespace CustomersApi.DataStore;
5+
6+
public class CustomerDbContext : DbContext
57
{
6-
public class CustomerDbContext : DbContext
8+
public CustomerDbContext(DbContextOptions<CustomerDbContext> options)
9+
: base(options)
710
{
8-
public CustomerDbContext(DbContextOptions<CustomerDbContext> options)
9-
: base(options)
10-
{
11-
}
11+
}
1212

13-
public DbSet<Customer> Customers { get; set; }
13+
public DbSet<Customer> Customers => Set<Customer>();
1414

15-
public void Seed()
15+
public void Seed()
16+
{
17+
if (Database.EnsureCreated())
1618
{
17-
if (Database.EnsureCreated())
18-
{
19-
Database.Migrate();
19+
Database.Migrate();
2020

21-
Customers.Add(new Customer(1, "Marcel Belding"));
22-
Customers.Add(new Customer(2, "Phyllis Schriver"));
23-
Customers.Add(new Customer(3, "Estefana Balderrama"));
24-
Customers.Add(new Customer(4, "Kenyetta Lone"));
25-
Customers.Add(new Customer(5, "Vernita Fernald"));
26-
Customers.Add(new Customer(6, "Tessie Storrs"));
21+
Customers.Add(new Customer(1, "Marcel Belding"));
22+
Customers.Add(new Customer(2, "Phyllis Schriver"));
23+
Customers.Add(new Customer(3, "Estefana Balderrama"));
24+
Customers.Add(new Customer(4, "Kenyetta Lone"));
25+
Customers.Add(new Customer(5, "Vernita Fernald"));
26+
Customers.Add(new Customer(6, "Tessie Storrs"));
2727

28-
SaveChanges();
29-
}
28+
SaveChanges();
3029
}
3130
}
3231
}
Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
1-
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Extensions.Hosting;
1+
using CustomersApi.DataStore;
2+
using Microsoft.EntityFrameworkCore;
43
using Shared;
54

6-
namespace Samples.CustomersApi
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
9+
builder.WebHost.UseUrls(Constants.CustomersUrl);
10+
11+
// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
12+
builder.Services.AddJaeger();
13+
14+
// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
15+
builder.Services.AddOpenTracing(ot =>
716
{
8-
public class Program
17+
ot.ConfigureAspNetCore(options =>
918
{
10-
public static void Main(string[] args)
11-
{
12-
CreateHostBuilder(args).Build().Run();
13-
}
14-
15-
public static IHostBuilder CreateHostBuilder(string[] args)
16-
{
17-
return Host.CreateDefaultBuilder(args)
18-
.ConfigureWebHostDefaults(webBuilder =>
19-
{
20-
webBuilder
21-
.UseStartup<Startup>()
22-
.UseUrls(Constants.CustomersUrl);
23-
})
24-
.ConfigureServices(services =>
25-
{
26-
// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
27-
services.AddJaeger();
28-
29-
// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
30-
services.AddOpenTracing(builder =>
31-
{
32-
builder.ConfigureAspNetCore(options =>
33-
{
34-
// We don't need any tracing data for our health endpoint.
35-
options.Hosting.IgnorePatterns.Add(ctx => ctx.Request.Path == "/health");
36-
});
37-
38-
builder.ConfigureEntityFrameworkCore(options =>
39-
{
40-
// This is an example for how certain EF Core commands can be ignored.
41-
// As en example, we're ignoring the "PRAGMA foreign_keys=ON;" commands that are executed by Sqlite.
42-
// Remove this code to see those statements.
43-
options.IgnorePatterns.Add(cmd => cmd.Command.CommandText.StartsWith("PRAGMA"));
44-
});
45-
});
46-
47-
});
48-
}
49-
}
19+
// We don't need any tracing data for our health endpoint.
20+
options.Hosting.IgnorePatterns.Add(ctx => ctx.Request.Path == "/health");
21+
});
22+
23+
ot.ConfigureEntityFrameworkCore(options =>
24+
{
25+
// This is an example for how certain EF Core commands can be ignored.
26+
// As en example, we're ignoring the "PRAGMA foreign_keys=ON;" commands that are executed by Sqlite.
27+
// Remove this code to see those statements.
28+
options.IgnorePatterns.Add(cmd => cmd.Command.CommandText.StartsWith("PRAGMA"));
29+
});
30+
});
31+
32+
// Adds a Sqlite DB to show EFCore traces.
33+
builder.Services.AddDbContext<CustomerDbContext>(options =>
34+
{
35+
options.UseSqlite("Data Source=DataStore/customers.db");
36+
});
37+
38+
builder.Services.AddHealthChecks()
39+
.AddDbContextCheck<CustomerDbContext>();
40+
41+
42+
var app = builder.Build();
43+
44+
45+
// Load some dummy data into the db.
46+
using (var scope = app.Services.CreateScope())
47+
{
48+
var dbContext = scope.ServiceProvider.GetRequiredService<CustomerDbContext>();
49+
dbContext.Seed();
5050
}
51+
52+
53+
// Configure the HTTP request pipeline.
54+
55+
app.MapGet("/", () => "Customers API");
56+
57+
app.MapHealthChecks("/health");
58+
59+
app.MapGet("/customers", async (CustomerDbContext dbContext) => await dbContext.Customers.ToListAsync());
60+
61+
app.MapGet("/customers/{id}", async (int id, CustomerDbContext dbContext, ILogger<Program> logger) =>
62+
{
63+
var customer = await dbContext.Customers.FirstOrDefaultAsync(x => x.CustomerId == id);
64+
65+
if (customer == null)
66+
return Results.NotFound();
67+
68+
// ILogger events are sent to OpenTracing as well!
69+
logger.LogInformation("Returning data for customer {CustomerId}", id);
70+
71+
return Results.Ok(customer);
72+
});
73+
74+
app.Run();

samples/net7.0/CustomersApi/Startup.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)