From 2e8313301b4f790530d33b352af9e5b5f973ac2c Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Tue, 2 Feb 2021 22:49:37 -0500 Subject: [PATCH] better DI --- .../Processors/RetrieveTweetsProcessor.cs | 13 ++++--------- src/BirdsiteLive.Twitter/CachedTwitterService.cs | 7 ++++++- .../BirdsiteLive.Pipeline.Tests.csproj | 6 ++++-- .../Processors/RetrieveTweetsProcessorTests.cs | 6 +++--- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs b/src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs index c0976c5..e60c121 100644 --- a/src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs +++ b/src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs @@ -17,12 +17,12 @@ namespace BirdsiteLive.Pipeline.Processors public class RetrieveTweetsProcessor : IRetrieveTweetsProcessor { private readonly ITwitterTweetsService _twitterTweetsService; - private readonly ITwitterUserService _twitterUserService; + private readonly ICachedTwitterUserService _twitterUserService; private readonly ITwitterUserDal _twitterUserDal; private readonly ILogger _logger; #region Ctor - public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal, ITwitterUserService twitterUserService, ILogger logger) + public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal, ICachedTwitterUserService twitterUserService, ILogger logger) { _twitterTweetsService = twitterTweetsService; _twitterUserDal = twitterUserDal; @@ -77,13 +77,8 @@ private ExtractedTweet[] RetrieveNewTweets(SyncTwitterUser user) } catch (Exception e) { - _logger.LogError(e, "Error retrieving TL of {Username} from {LastTweetPostedId}", user.Acct, user.LastTweetPostedId); - - if (_twitterUserService is CachedTwitterUserService service) - { - _logger.LogInformation("Purge {Username} from cache", user.Acct); - service.PurgeUser(user.Acct); - } + _logger.LogError(e, "Error retrieving TL of {Username} from {LastTweetPostedId}, purging user from cache", user.Acct, user.LastTweetPostedId); + _twitterUserService.PurgeUser(user.Acct); } return tweets; diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index d8ca1fb..d9b90d2 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -4,7 +4,12 @@ namespace BirdsiteLive.Twitter { - public class CachedTwitterUserService : ITwitterUserService + public interface ICachedTwitterUserService : ITwitterUserService + { + void PurgeUser(string username); + } + + public class CachedTwitterUserService : ICachedTwitterUserService { private readonly ITwitterUserService _twitterService; diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/BirdsiteLive.Pipeline.Tests.csproj b/src/Tests/BirdsiteLive.Pipeline.Tests/BirdsiteLive.Pipeline.Tests.csproj index aa7750b..d1cfd06 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/BirdsiteLive.Pipeline.Tests.csproj +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/BirdsiteLive.Pipeline.Tests.csproj @@ -15,11 +15,13 @@ - + - + + + diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RetrieveTweetsProcessorTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RetrieveTweetsProcessorTests.cs index b35a688..02fd7bd 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RetrieveTweetsProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RetrieveTweetsProcessorTests.cs @@ -61,7 +61,7 @@ public async Task ProcessAsync_UserNotSync_Test() )) .Returns(Task.CompletedTask); - var twitterUserServiceMock = new Mock(MockBehavior.Strict); + var twitterUserServiceMock = new Mock(MockBehavior.Strict); var logger = new Mock>(MockBehavior.Strict); #endregion @@ -125,7 +125,7 @@ public async Task ProcessAsync_UserSync_Test() var twitterUserDalMock = new Mock(MockBehavior.Strict); - var twitterUserServiceMock = new Mock(MockBehavior.Strict); + var twitterUserServiceMock = new Mock(MockBehavior.Strict); var logger = new Mock>(MockBehavior.Strict); #endregion @@ -192,7 +192,7 @@ public async Task ProcessAsync_UserPartiallySync_Test() var twitterUserDalMock = new Mock(MockBehavior.Strict); - var twitterUserServiceMock = new Mock(MockBehavior.Strict); + var twitterUserServiceMock = new Mock(MockBehavior.Strict); var logger = new Mock>(MockBehavior.Strict); #endregion