Skip to content

Commit

Permalink
formatting program.cs files in samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Feb 6, 2024
1 parent 7181663 commit 8d297e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
20 changes: 16 additions & 4 deletions sample/Ardalis.Sample.App1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@
app.UseHttpsRedirection();


app.MapGet("/customers", async (IRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
app.MapGet("/customers", async (IRepository<Customer> repo,
IMapper mapper,
CancellationToken cancellationToken) =>
{
var spec = new CustomerSpec();
var customers = await repo.ListAsync(spec, cancellationToken);
var customersDto = mapper.Map<List<CustomerDto>>(customers);
return Results.Ok(customersDto);
});

app.MapGet("/customers/{id}", async (IRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customers/{id}", async (IRepository<Customer> repo,
IMapper mapper,
int id,
CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
Expand All @@ -45,7 +50,10 @@
return Results.Ok(customerDto);
});

app.MapPost("/customers", async (IRepository<Customer> repo, IMapper mapper, CustomerCreateDto customerCreateDto, CancellationToken cancellationToken) =>
app.MapPost("/customers", async (IRepository<Customer> repo,
IMapper mapper,
CustomerCreateDto customerCreateDto,
CancellationToken cancellationToken) =>
{
var customer = new Customer
{
Expand All @@ -58,7 +66,11 @@
return Results.Ok(customerDto);
});

app.MapPut("/customers/{id}", async (IRepository<Customer> repo, IMapper mapper, int id, CustomerUpdateDto customerUpdate, CancellationToken cancellationToken) =>
app.MapPut("/customers/{id}", async (IRepository<Customer> repo,
IMapper mapper,
int id,
CustomerUpdateDto customerUpdate,
CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
Expand Down
24 changes: 19 additions & 5 deletions sample/Ardalis.Sample.App2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
app.UseHttpsRedirection();


app.MapGet("/customers", async (IReadRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
app.MapGet("/customers", async (IReadRepository<Customer> repo,
IMapper mapper,
CancellationToken cancellationToken) =>
{
var spec = new CustomerSpec();
var customers = await repo.ListAsync(spec, cancellationToken);
var customersDto = mapper.Map<List<CustomerDto>>(customers);
return Results.Ok(customersDto);
});

app.MapGet("/customers/{id}", async (IReadRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customers/{id}", async (IReadRepository<Customer> repo,
IMapper mapper,
int id,
CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
// If you want to use the SingleOrDefault methods, the specification must inherit from SingleResultSpecification
Expand All @@ -49,7 +54,10 @@
});

// Using the specification directly with the dbContext (no repositories).
app.MapGet("/customers/v2/{id}", async (AppDbContext dbContext, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customers/v2/{id}", async (AppDbContext dbContext,
IMapper mapper,
int id,
CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await dbContext.Customers
Expand All @@ -61,7 +69,10 @@
});

// In this version, we're projecting the result to a DTO directly in the specification
app.MapGet("/customers/v3/{id}", async (IReadRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customers/v3/{id}", async (IReadRepository<Customer> repo,
IMapper mapper,
int id,
CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdProjectionSpec(id);
var customerDto = await repo.FirstOrDefaultAsync(spec, cancellationToken);
Expand All @@ -70,7 +81,10 @@
});

// We're selecting only a name from Customer.
app.MapGet("/customer-names/{id}", async (IReadRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customer-names/{id}", async (IReadRepository<Customer> repo,
IMapper mapper,
int id,
CancellationToken cancellationToken) =>
{
var spec = new CustomerNameSpec(id);
var name = await repo.FirstOrDefaultAsync(spec, cancellationToken);
Expand Down

0 comments on commit 8d297e5

Please sign in to comment.