Skip to content

Commit

Permalink
Example with injection in method instead of constructor (ardalis#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Jerndin <jonas@queenslab.se>
  • Loading branch information
swizkon and Jonas Jerndin authored Sep 23, 2020
1 parent 40b6fbe commit b0d1f0a
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions sample/SampleEndpointApp/AuthorEndpoints/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@ namespace SampleEndpointApp.Authors
{
public class List : BaseAsyncEndpoint
{
private readonly IAsyncRepository<Author> _repository;
private readonly IMapper _mapper;

public List(IAsyncRepository<Author> repository,
IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}

[HttpGet("/authors")]
[SwaggerOperation(
Summary = "List all Authors",
Description = "List all Authors",
OperationId = "Author.List",
Tags = new[] { "AuthorEndpoint" })
]
public async Task<ActionResult> HandleAsync([FromQuery] int page = 1, int perPage = 10, CancellationToken cancellationToken = default)
public async Task<ActionResult> HandleAsync(
[FromServices] IAsyncRepository<Author> repository,
[FromServices] IMapper mapper,
[FromQuery] int page = 1, int perPage = 10,
CancellationToken cancellationToken = default)
{
var result = (await _repository.ListAllAsync(perPage, page, cancellationToken))
.Select(i => _mapper.Map<AuthorListResult>(i));
var result = (await repository.ListAllAsync(perPage, page, cancellationToken))
.Select(i => mapper.Map<AuthorListResult>(i));

return Ok(result);
}
}
}
}

0 comments on commit b0d1f0a

Please sign in to comment.