diff --git a/CryptoExchange.Net/Clients/CryptoExchangeClient.cs b/CryptoExchange.Net/Clients/CryptoExchangeClient.cs new file mode 100644 index 0000000..61b3874 --- /dev/null +++ b/CryptoExchange.Net/Clients/CryptoExchangeClient.cs @@ -0,0 +1,44 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + public class CryptoExchangeClient : ICryptoExchangeClient, IDisposable + { + private Dictionary _serviceCache = new Dictionary(); + + private readonly IServiceProvider _serviceProvider; + + public CryptoExchangeClient(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + _serviceCache = new Dictionary(); + } + + public IEnumerable GetSpotClients() + { + return _serviceProvider.GetServices().ToList(); + } + + public T? TryGet() + { + var type = typeof(T); + if (_serviceCache.TryGetValue(type, out var value)) + return (T?)value; + + var result = _serviceProvider.GetService(); + _serviceCache.Add(type, result); + return result; + } + + public void Dispose() + { + _serviceCache.Clear(); + } + } +} diff --git a/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs b/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs new file mode 100644 index 0000000..1223984 --- /dev/null +++ b/CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs @@ -0,0 +1,15 @@ +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Interfaces.CommonClients; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptoExchange.Net.Clients +{ + public interface ICryptoExchangeClient + { + IEnumerable GetSpotClients(); + T? TryGet(); + } +}