From bf1ead29205878526573496653554ae81c3ebde7 Mon Sep 17 00:00:00 2001 From: Kevin Dockx Date: Tue, 25 Sep 2018 16:16:57 +0200 Subject: [PATCH] Improve DI for legacy sample --- .../Books.Api/Controllers/BooksController.cs | 19 ++++++++++++------- .../Migrations/InitialMigration.Designer.cs | 2 +- Books/Books.Api/Services/BooksRepository.cs | 3 +-- Books/Books.Api/Startup.cs | 3 +++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Books/Books.Api/Controllers/BooksController.cs b/Books/Books.Api/Controllers/BooksController.cs index ee74428..1292571 100644 --- a/Books/Books.Api/Controllers/BooksController.cs +++ b/Books/Books.Api/Controllers/BooksController.cs @@ -1,6 +1,7 @@ using AutoMapper; using Books.Api.Models; using Books.Api.Services; +using Books.Legacy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; @@ -15,14 +16,18 @@ public class BooksController : ControllerBase private readonly IBooksRepository _booksRepository; private readonly IMapper _mapper; private readonly ILogger _logger; + private readonly ComplicatedPageCalculator _complicatedPageCalculator; public BooksController(IBooksRepository booksRepository, - IMapper mapper, ILogger logger) + IMapper mapper, ILogger logger, + ComplicatedPageCalculator complicatedPageCalculator) { _booksRepository = booksRepository ?? throw new ArgumentNullException(nameof(booksRepository)); _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _complicatedPageCalculator = complicatedPageCalculator ?? + throw new ArgumentNullException(nameof(complicatedPageCalculator)); } [HttpGet] @@ -39,12 +44,12 @@ public async Task GetBook(Guid id) // calculate the book pages // DON'T DO THIS, this is sample code for a bad practice! - _logger.LogInformation($"ThreadId when entering GetBookAsync: {System.Threading.Thread.CurrentThread.ManagedThreadId}"); + _logger.LogInformation($"ThreadId when entering GetBook: " + + $"{System.Threading.Thread.CurrentThread.ManagedThreadId}"); var bookPages = await GetBookPages(id); // good way to call legacy computational-bound code is by NOT offloading it to the background - //var pageCalculator = new Books.Legacy.ComplicatedPageCalculator(); - //var bookPages = pageCalculator.CalculateBookPages(id); + //var bookPages = _complicatedPageCalculator.CalculateBookPages(id); return Ok(_mapper.Map(bookEntity)); } @@ -59,10 +64,10 @@ private Task GetBookPages(Guid id) { return Task.Run(() => { - _logger.LogInformation($"ThreadId when calculating the amount of pages: {System.Threading.Thread.CurrentThread.ManagedThreadId}"); + _logger.LogInformation($"ThreadId when calculating the amount of pages: " + + $"{System.Threading.Thread.CurrentThread.ManagedThreadId}"); - var pageCalculator = new Books.Legacy.ComplicatedPageCalculator(); - return pageCalculator.CalculateBookPages(id); + return _complicatedPageCalculator.CalculateBookPages(id); }); } diff --git a/Books/Books.Api/Migrations/InitialMigration.Designer.cs b/Books/Books.Api/Migrations/InitialMigration.Designer.cs index 010d933..79b80f2 100644 --- a/Books/Books.Api/Migrations/InitialMigration.Designer.cs +++ b/Books/Books.Api/Migrations/InitialMigration.Designer.cs @@ -10,7 +10,7 @@ namespace Books.Api.Migrations { [DbContext(typeof(BooksContext))] - [Migration("20180717132912_InitialMigration")] + [Migration("InitialMigration")] partial class InitialMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Books/Books.Api/Services/BooksRepository.cs b/Books/Books.Api/Services/BooksRepository.cs index 84f37bf..34d6ce6 100644 --- a/Books/Books.Api/Services/BooksRepository.cs +++ b/Books/Books.Api/Services/BooksRepository.cs @@ -3,7 +3,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; -using System.Net.Http; using System.Threading.Tasks; namespace Books.Api.Services @@ -13,7 +12,7 @@ public class BooksRepository : IBooksRepository, IDisposable private BooksContext _context; private readonly ILogger _logger; - public BooksRepository(BooksContext context, IHttpClientFactory httpClientFactory, + public BooksRepository(BooksContext context, ILogger logger) { _context = context ?? throw new ArgumentNullException(nameof(context)); diff --git a/Books/Books.Api/Startup.cs b/Books/Books.Api/Startup.cs index b496f6c..1536a4a 100644 --- a/Books/Books.Api/Startup.cs +++ b/Books/Books.Api/Startup.cs @@ -1,6 +1,7 @@ using AutoMapper; using Books.Api.Contexts; using Books.Api.Services; +using Books.Legacy; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -32,6 +33,8 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); + services.AddTransient(); + services.AddAutoMapper(); }