Skip to content

Commit

Permalink
Removed static AutoMapper setup and replaced with instanced AutoMappe…
Browse files Browse the repository at this point in the history
…r. Added helper methods to support Microsoft's dependency injection container.
  • Loading branch information
babelshift committed May 1, 2020
1 parent 7f708b1 commit 5e10726
Show file tree
Hide file tree
Showing 32 changed files with 467 additions and 324 deletions.
7 changes: 6 additions & 1 deletion src/Steam.UnitTests/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using SteamWebAPI2.Utilities;
using Microsoft.Extensions.Options;

namespace Steam.UnitTests
{
Expand All @@ -15,7 +16,11 @@ public BaseTest()
.AddUserSecrets<CSGOServersTests>();
configuration = builder.Build();

factory = new SteamWebInterfaceFactory(configuration["SteamWebApiKey"]);
var factoryOptions = new SteamWebInterfaceFactoryOptions()
{
SteamWebApiKey = configuration["SteamWebApiKey"]
};
factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
}
}
}
1 change: 1 addition & 0 deletions src/Steam.UnitTests/Steam.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
Expand Down
28 changes: 23 additions & 5 deletions src/Steam.UnitTests/SteamWebInterfaceFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SteamWebAPI2.Interfaces;
using Microsoft.Extensions.Options;
using SteamWebAPI2.Interfaces;
using SteamWebAPI2.Utilities;
using System;
using System.Net.Http;
Expand All @@ -8,19 +9,36 @@ namespace Steam.UnitTests
{
public class SteamWebInterfaceFactoryTests
{
private readonly SteamWebInterfaceFactory factory = new SteamWebInterfaceFactory("ABC123");
private readonly SteamWebInterfaceFactory factory;

public SteamWebInterfaceFactoryTests()
{
var factoryOptions = new SteamWebInterfaceFactoryOptions()
{
SteamWebApiKey = "ABC123"
};
factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
}

[Fact]
public void Constructor_Should_Succeed()
{
var factory = new SteamWebInterfaceFactory("ABC123");
var factoryOptions = new SteamWebInterfaceFactoryOptions()
{
SteamWebApiKey = "ABC123"
};
var factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
Assert.NotNull(factory);
}

[Fact]
public void Constructor_Should_Fail_If_Empty_Key()
{
Assert.Throws<ArgumentNullException>(() => new SteamWebInterfaceFactory(""));
var factoryOptions = new SteamWebInterfaceFactoryOptions()
{
SteamWebApiKey = ""
};
Assert.Throws<ArgumentNullException>(() => new SteamWebInterfaceFactory(Options.Create(factoryOptions)));
}

[Fact]
Expand Down Expand Up @@ -130,7 +148,7 @@ public void Create_SteamRemoteStorage_Interface_Should_Succeed()
[Fact]
public void Create_SteamStore_Interface_Should_Succeed()
{
var steamInterface = new SteamStore();
var steamInterface = factory.CreateSteamStoreInterface();
Assert.NotNull(steamInterface);
}

Expand Down
169 changes: 0 additions & 169 deletions src/SteamWebAPI2/AutoMapperConfiguration.cs

This file was deleted.

15 changes: 10 additions & 5 deletions src/SteamWebAPI2/Interfaces/CSGOServers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Steam.Models.CSGO;
using AutoMapper;
using Steam.Models.CSGO;
using SteamWebAPI2.Models.CSGO;
using SteamWebAPI2.Utilities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -11,14 +13,17 @@ namespace SteamWebAPI2.Interfaces
/// </summary>
public class CSGOServers : ICSGOServers
{
private ISteamWebInterface steamWebInterface;
private readonly IMapper mapper;
private readonly ISteamWebInterface steamWebInterface;

/// <summary>
/// Default constructor established the Steam Web API key and initializes for subsequent method calls
/// </summary>
/// <param name="steamWebApiKey"></param>
public CSGOServers(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
public CSGOServers(IMapper mapper, ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
{
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));

this.steamWebInterface = steamWebInterface == null
? new SteamWebInterface("ICSGOServers_730", steamWebRequest)
: steamWebInterface;
Expand All @@ -42,7 +47,7 @@ GameMapsPlaytimeMapGroup mapGroup

var steamWebResponse = await steamWebInterface.GetAsync<GameMapsPlaytimeContainer>("GetGameMapsPlaytime", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<
var steamWebResponseModel = mapper.Map<
ISteamWebResponse<GameMapsPlaytimeContainer>,
ISteamWebResponse<IEnumerable<GameMapsPlaytimeModel>>>(steamWebResponse);

Expand All @@ -57,7 +62,7 @@ public async Task<ISteamWebResponse<ServerStatusModel>> GetGameServerStatusAsync
{
var steamWebResponse = await steamWebInterface.GetAsync<ServerStatusResultContainer>("GetGameServersStatus", 1);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<ServerStatusResultContainer>, ISteamWebResponse<ServerStatusModel>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<ServerStatusResultContainer>, ISteamWebResponse<ServerStatusModel>>(steamWebResponse);

return steamWebResponseModel;
}
Expand Down
21 changes: 12 additions & 9 deletions src/SteamWebAPI2/Interfaces/DOTA2Econ.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Steam.Models.DOTA2;
using AutoMapper;
using SteamWebAPI2.Models.DOTA2;
using SteamWebAPI2.Utilities;
using System;
Expand All @@ -12,15 +12,18 @@ namespace SteamWebAPI2.Interfaces
/// </summary>
public class DOTA2Econ : IDOTA2Econ
{
private ISteamWebInterface dota2WebInterface;
private ISteamWebInterface dota2TestWebInterface;
private readonly ISteamWebInterface dota2WebInterface;
private readonly ISteamWebInterface dota2TestWebInterface;
private readonly IMapper mapper;

/// <summary>
/// Default constructor established the Steam Web API key and initializes for subsequent method calls
/// </summary>
/// <param name="steamWebApiKey"></param>
public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
public DOTA2Econ(IMapper mapper, ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
{
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));

this.dota2WebInterface = steamWebInterface == null
? new SteamWebInterface("IEconDOTA2_570", steamWebRequest)
: steamWebInterface;
Expand All @@ -41,7 +44,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn

var steamWebResponse = await dota2WebInterface.GetAsync<GameItemResultContainer>("GetGameItems", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<GameItemResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.GameItem>>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<GameItemResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.GameItem>>>(steamWebResponse);

return steamWebResponseModel;
}
Expand All @@ -63,7 +66,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn

var steamWebResponse = await dota2WebInterface.GetAsync<HeroResultContainer>("GetHeroes", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<HeroResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.Hero>>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<HeroResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.Hero>>>(steamWebResponse);

return steamWebResponseModel;
}
Expand All @@ -81,7 +84,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn

var steamWebResponse = await dota2WebInterface.GetAsync<RarityResultContainer>("GetRarities", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<RarityResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.Rarity>>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<RarityResultContainer>, ISteamWebResponse<IReadOnlyCollection<Steam.Models.DOTA2.Rarity>>>(steamWebResponse);

return steamWebResponseModel;
}
Expand All @@ -99,7 +102,7 @@ public async Task<ISteamWebResponse<uint>> GetTournamentPrizePoolAsync(uint? lea

var steamWebResponse = await dota2WebInterface.GetAsync<PrizePoolResultContainer>("GetTournamentPrizePool", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<PrizePoolResultContainer>, ISteamWebResponse<uint>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<PrizePoolResultContainer>, ISteamWebResponse<uint>>(steamWebResponse);

return steamWebResponseModel;
}
Expand All @@ -124,7 +127,7 @@ public async Task<ISteamWebResponse<string>> GetItemIconPathAsync(string iconNam

var steamWebResponse = await dota2TestWebInterface.GetAsync<ItemIconPathResultContainer>("GetItemIconPath", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<ItemIconPathResultContainer>, ISteamWebResponse<string>>(steamWebResponse);
var steamWebResponseModel = mapper.Map<ISteamWebResponse<ItemIconPathResultContainer>, ISteamWebResponse<string>>(steamWebResponse);

return steamWebResponseModel;
}
Expand Down
Loading

0 comments on commit 5e10726

Please sign in to comment.