Skip to content

Commit

Permalink
better DI
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Feb 3, 2021
1 parent 299ad64 commit 2e83133
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
13 changes: 4 additions & 9 deletions src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RetrieveTweetsProcessor> _logger;

#region Ctor
public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal, ITwitterUserService twitterUserService, ILogger<RetrieveTweetsProcessor> logger)
public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal, ICachedTwitterUserService twitterUserService, ILogger<RetrieveTweetsProcessor> logger)
{
_twitterTweetsService = twitterTweetsService;
_twitterUserDal = twitterUserDal;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/BirdsiteLive.Twitter/CachedTwitterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BirdsiteLive.Pipeline\BirdsiteLive.Pipeline.csproj" />
<Folder Include="Tools\" />
</ItemGroup>

<ItemGroup>
<Folder Include="Tools\" />
<ProjectReference Include="..\..\BirdsiteLive.Pipeline\BirdsiteLive.Pipeline.csproj" />
<ProjectReference Include="..\..\BirdsiteLive.Twitter\BirdsiteLive.Twitter.csproj" />
<ProjectReference Include="..\..\DataAccessLayers\BirdsiteLive.DAL\BirdsiteLive.DAL.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task ProcessAsync_UserNotSync_Test()
))
.Returns(Task.CompletedTask);

var twitterUserServiceMock = new Mock<ITwitterUserService>(MockBehavior.Strict);
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);

var logger = new Mock<ILogger<RetrieveTweetsProcessor>>(MockBehavior.Strict);
#endregion
Expand Down Expand Up @@ -125,7 +125,7 @@ public async Task ProcessAsync_UserSync_Test()

var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);

var twitterUserServiceMock = new Mock<ITwitterUserService>(MockBehavior.Strict);
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);

var logger = new Mock<ILogger<RetrieveTweetsProcessor>>(MockBehavior.Strict);
#endregion
Expand Down Expand Up @@ -192,7 +192,7 @@ public async Task ProcessAsync_UserPartiallySync_Test()

var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);

var twitterUserServiceMock = new Mock<ITwitterUserService>(MockBehavior.Strict);
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);

var logger = new Mock<ILogger<RetrieveTweetsProcessor>>(MockBehavior.Strict);
#endregion
Expand Down

0 comments on commit 2e83133

Please sign in to comment.