Skip to content

Commit

Permalink
Add example of service dependency injection (ardalis#36)
Browse files Browse the repository at this point in the history
* Add example of service dependency injection

* Add some indentation to new code example

* Copy example code from the sample app
  • Loading branch information
swizkon authored Sep 24, 2020
1 parent b0d1f0a commit dcf2664
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ public override async Task<ActionResult<CreateAuthorResult>> HandleAsync([FromBo
return Ok(result);
}
```
Option to use service dependency injection instead of constructor
``` csharp
// File: sample/SampleEndpointApp/AuthorEndpoints/List.cs
public class List : BaseAsyncEndpoint
{
[HttpGet("/authors")]
[SwaggerOperation(
Summary = "List all Authors",
Description = "List all Authors",
OperationId = "Author.List",
Tags = new[] { "AuthorEndpoint" })
]
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));

return Ok(result);
}
}
```

Examples of the configuration can be found in the sample API project

Expand Down

0 comments on commit dcf2664

Please sign in to comment.