|
1 | | -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System.Threading.Tasks; |
5 | 5 | using Benchmarks.Data; |
6 | 6 | using Microsoft.AspNetCore.Mvc; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
7 | 8 |
|
8 | 9 | namespace Benchmarks.Controllers |
9 | 10 | { |
10 | 11 | [Route("mvc")] |
11 | 12 | public class FortunesController : Controller |
12 | 13 | { |
13 | | - private readonly RawDb _rawDb; |
14 | | - private readonly DapperDb _dapperDb; |
15 | | - private readonly EfDb _efDb; |
| 14 | + private RawDb _rawDb; |
| 15 | + private DapperDb _dapperDb; |
| 16 | + private EfDb _efDb; |
16 | 17 |
|
17 | | - public FortunesController(RawDb rawDb, DapperDb dapperDb, EfDb efDb) |
| 18 | + private RawDb RawDb |
18 | 19 | { |
19 | | - _rawDb = rawDb; |
20 | | - _dapperDb = dapperDb; |
21 | | - _efDb = efDb; |
| 20 | + get |
| 21 | + { |
| 22 | + if (_rawDb == null) |
| 23 | + { |
| 24 | + _rawDb = HttpContext.RequestServices.GetRequiredService<RawDb>(); |
| 25 | + } |
| 26 | + |
| 27 | + return _rawDb; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private DapperDb DapperDb |
| 32 | + { |
| 33 | + get |
| 34 | + { |
| 35 | + if (_dapperDb == null) |
| 36 | + { |
| 37 | + _dapperDb = HttpContext.RequestServices.GetRequiredService<DapperDb>(); |
| 38 | + } |
| 39 | + |
| 40 | + return _dapperDb; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private EfDb EfDb |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + if (_efDb == null) |
| 49 | + { |
| 50 | + _efDb = HttpContext.RequestServices.GetRequiredService<EfDb>(); |
| 51 | + } |
| 52 | + |
| 53 | + return _efDb; |
| 54 | + } |
22 | 55 | } |
23 | 56 |
|
24 | 57 | [HttpGet("fortunes/raw")] |
25 | 58 | public async Task<IActionResult> Raw() |
26 | 59 | { |
27 | | - return View("Fortunes", await _rawDb.LoadFortunesRows()); |
| 60 | + return View("Fortunes", await RawDb.LoadFortunesRows()); |
28 | 61 | } |
29 | 62 |
|
30 | 63 | [HttpGet("fortunes/dapper")] |
31 | 64 | public async Task<IActionResult> Dapper() |
32 | 65 | { |
33 | | - return View("Fortunes", await _dapperDb.LoadFortunesRows()); |
| 66 | + return View("Fortunes", await DapperDb.LoadFortunesRows()); |
34 | 67 | } |
35 | 68 |
|
36 | 69 | [HttpGet("fortunes/ef")] |
37 | 70 | public async Task<IActionResult> Ef() |
38 | 71 | { |
39 | | - return View("Fortunes", await _efDb.LoadFortunesRows()); |
| 72 | + return View("Fortunes", await EfDb.LoadFortunesRows()); |
40 | 73 | } |
41 | 74 | } |
42 | 75 | } |
0 commit comments