Skip to content

Commit 5e10726

Browse files
author
Justin
committed
Removed static AutoMapper setup and replaced with instanced AutoMapper. Added helper methods to support Microsoft's dependency injection container.
1 parent 7f708b1 commit 5e10726

32 files changed

+467
-324
lines changed

src/Steam.UnitTests/BaseTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Configuration;
22
using SteamWebAPI2.Utilities;
3+
using Microsoft.Extensions.Options;
34

45
namespace Steam.UnitTests
56
{
@@ -15,7 +16,11 @@ public BaseTest()
1516
.AddUserSecrets<CSGOServersTests>();
1617
configuration = builder.Build();
1718

18-
factory = new SteamWebInterfaceFactory(configuration["SteamWebApiKey"]);
19+
var factoryOptions = new SteamWebInterfaceFactoryOptions()
20+
{
21+
SteamWebApiKey = configuration["SteamWebApiKey"]
22+
};
23+
factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
1924
}
2025
}
2126
}

src/Steam.UnitTests/Steam.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
10+
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.3" />
1011
<PackageReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.2" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1213
<PackageReference Include="xunit" Version="2.4.0" />

src/Steam.UnitTests/SteamWebInterfaceFactoryTests.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SteamWebAPI2.Interfaces;
1+
using Microsoft.Extensions.Options;
2+
using SteamWebAPI2.Interfaces;
23
using SteamWebAPI2.Utilities;
34
using System;
45
using System.Net.Http;
@@ -8,19 +9,36 @@ namespace Steam.UnitTests
89
{
910
public class SteamWebInterfaceFactoryTests
1011
{
11-
private readonly SteamWebInterfaceFactory factory = new SteamWebInterfaceFactory("ABC123");
12+
private readonly SteamWebInterfaceFactory factory;
13+
14+
public SteamWebInterfaceFactoryTests()
15+
{
16+
var factoryOptions = new SteamWebInterfaceFactoryOptions()
17+
{
18+
SteamWebApiKey = "ABC123"
19+
};
20+
factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
21+
}
1222

1323
[Fact]
1424
public void Constructor_Should_Succeed()
1525
{
16-
var factory = new SteamWebInterfaceFactory("ABC123");
26+
var factoryOptions = new SteamWebInterfaceFactoryOptions()
27+
{
28+
SteamWebApiKey = "ABC123"
29+
};
30+
var factory = new SteamWebInterfaceFactory(Options.Create(factoryOptions));
1731
Assert.NotNull(factory);
1832
}
1933

2034
[Fact]
2135
public void Constructor_Should_Fail_If_Empty_Key()
2236
{
23-
Assert.Throws<ArgumentNullException>(() => new SteamWebInterfaceFactory(""));
37+
var factoryOptions = new SteamWebInterfaceFactoryOptions()
38+
{
39+
SteamWebApiKey = ""
40+
};
41+
Assert.Throws<ArgumentNullException>(() => new SteamWebInterfaceFactory(Options.Create(factoryOptions)));
2442
}
2543

2644
[Fact]
@@ -130,7 +148,7 @@ public void Create_SteamRemoteStorage_Interface_Should_Succeed()
130148
[Fact]
131149
public void Create_SteamStore_Interface_Should_Succeed()
132150
{
133-
var steamInterface = new SteamStore();
151+
var steamInterface = factory.CreateSteamStoreInterface();
134152
Assert.NotNull(steamInterface);
135153
}
136154

src/SteamWebAPI2/AutoMapperConfiguration.cs

Lines changed: 0 additions & 169 deletions
This file was deleted.

src/SteamWebAPI2/Interfaces/CSGOServers.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Steam.Models.CSGO;
1+
using AutoMapper;
2+
using Steam.Models.CSGO;
23
using SteamWebAPI2.Models.CSGO;
34
using SteamWebAPI2.Utilities;
5+
using System;
46
using System.Collections.Generic;
57
using System.Threading.Tasks;
68

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

1619
/// <summary>
1720
/// Default constructor established the Steam Web API key and initializes for subsequent method calls
1821
/// </summary>
1922
/// <param name="steamWebApiKey"></param>
20-
public CSGOServers(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
23+
public CSGOServers(IMapper mapper, ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
2124
{
25+
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
26+
2227
this.steamWebInterface = steamWebInterface == null
2328
? new SteamWebInterface("ICSGOServers_730", steamWebRequest)
2429
: steamWebInterface;
@@ -42,7 +47,7 @@ GameMapsPlaytimeMapGroup mapGroup
4247

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

45-
var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<
50+
var steamWebResponseModel = mapper.Map<
4651
ISteamWebResponse<GameMapsPlaytimeContainer>,
4752
ISteamWebResponse<IEnumerable<GameMapsPlaytimeModel>>>(steamWebResponse);
4853

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

60-
var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<ServerStatusResultContainer>, ISteamWebResponse<ServerStatusModel>>(steamWebResponse);
65+
var steamWebResponseModel = mapper.Map<ISteamWebResponse<ServerStatusResultContainer>, ISteamWebResponse<ServerStatusModel>>(steamWebResponse);
6166

6267
return steamWebResponseModel;
6368
}

src/SteamWebAPI2/Interfaces/DOTA2Econ.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Steam.Models.DOTA2;
1+
using AutoMapper;
22
using SteamWebAPI2.Models.DOTA2;
33
using SteamWebAPI2.Utilities;
44
using System;
@@ -12,15 +12,18 @@ namespace SteamWebAPI2.Interfaces
1212
/// </summary>
1313
public class DOTA2Econ : IDOTA2Econ
1414
{
15-
private ISteamWebInterface dota2WebInterface;
16-
private ISteamWebInterface dota2TestWebInterface;
15+
private readonly ISteamWebInterface dota2WebInterface;
16+
private readonly ISteamWebInterface dota2TestWebInterface;
17+
private readonly IMapper mapper;
1718

1819
/// <summary>
1920
/// Default constructor established the Steam Web API key and initializes for subsequent method calls
2021
/// </summary>
2122
/// <param name="steamWebApiKey"></param>
22-
public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
23+
public DOTA2Econ(IMapper mapper, ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebInterface = null)
2324
{
25+
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
26+
2427
this.dota2WebInterface = steamWebInterface == null
2528
? new SteamWebInterface("IEconDOTA2_570", steamWebRequest)
2629
: steamWebInterface;
@@ -41,7 +44,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn
4144

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

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

4649
return steamWebResponseModel;
4750
}
@@ -63,7 +66,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn
6366

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

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

6871
return steamWebResponseModel;
6972
}
@@ -81,7 +84,7 @@ public DOTA2Econ(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWebIn
8184

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

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

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

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

102-
var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<PrizePoolResultContainer>, ISteamWebResponse<uint>>(steamWebResponse);
105+
var steamWebResponseModel = mapper.Map<ISteamWebResponse<PrizePoolResultContainer>, ISteamWebResponse<uint>>(steamWebResponse);
103106

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

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

127-
var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<ISteamWebResponse<ItemIconPathResultContainer>, ISteamWebResponse<string>>(steamWebResponse);
130+
var steamWebResponseModel = mapper.Map<ISteamWebResponse<ItemIconPathResultContainer>, ISteamWebResponse<string>>(steamWebResponse);
128131

129132
return steamWebResponseModel;
130133
}

0 commit comments

Comments
 (0)