From 8a869e8e1db28b3ac2488f28714f8714cb891759 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 2 Feb 2024 16:55:28 +0100 Subject: [PATCH] wip --- .../Clients/CryptoExchangeClient.cs | 44 +++++++++++++++++++ .../Interfaces/ICryptoExchangeClient.cs | 15 +++++++ 2 files changed, 59 insertions(+) create mode 100644 CryptoExchange.Net/Clients/CryptoExchangeClient.cs create mode 100644 CryptoExchange.Net/Interfaces/ICryptoExchangeClient.cs 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(); + } +}