From 9ddd4468924ddc366895c2b7b39bf1118f210dff Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 6 Feb 2024 19:28:31 +0100 Subject: [PATCH] Some adjustment to the Crypto clients --- .../Clients/CryptoBaseClient.cs | 22 ++++++------------- .../Clients/CryptoRestClient.cs | 17 ++++++++++++-- .../Clients/CryptoSocketClient.cs | 12 +++++----- .../Interfaces/ICryptoRestClient.cs | 8 +++++++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/CryptoExchange.Net/Clients/CryptoBaseClient.cs b/CryptoExchange.Net/Clients/CryptoBaseClient.cs index d7db31e..e577721 100644 --- a/CryptoExchange.Net/Clients/CryptoBaseClient.cs +++ b/CryptoExchange.Net/Clients/CryptoBaseClient.cs @@ -1,10 +1,6 @@ -using CryptoExchange.Net.Interfaces; -using CryptoExchange.Net.Interfaces.CommonClients; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace CryptoExchange.Net.Clients { @@ -42,6 +38,10 @@ namespace CryptoExchange.Net.Clients /// public T TryGet(Func createFunc) { + var type = typeof(T); + if (_serviceCache.TryGetValue(type, out var value)) + return (T)value; + if (_serviceProvider == null) { // Create with default options @@ -50,16 +50,8 @@ namespace CryptoExchange.Net.Clients return createResult; } - var type = typeof(T); - if (_serviceCache.TryGetValue(type, out var value)) - return (T)value; - - var result = _serviceProvider.GetRequiredService(); - if (result == null) - { - // Does this mean the AddXX() hasn't been done? - } - + var result = _serviceProvider.GetService() + ?? throw new InvalidOperationException($"No service was found for {typeof(T).Name}, make sure the exchange is registered in dependency injection with the `services.Add[Exchange]()` method"); _serviceCache.Add(type, result!); return result; } diff --git a/CryptoExchange.Net/Clients/CryptoRestClient.cs b/CryptoExchange.Net/Clients/CryptoRestClient.cs index 7b51222..e5bc182 100644 --- a/CryptoExchange.Net/Clients/CryptoRestClient.cs +++ b/CryptoExchange.Net/Clients/CryptoRestClient.cs @@ -4,13 +4,19 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace CryptoExchange.Net.Clients { /// public class CryptoRestClient : CryptoBaseClient, ICryptoRestClient { + /// + /// ctor + /// + public CryptoRestClient() + { + } + /// /// ctor /// @@ -20,7 +26,7 @@ namespace CryptoExchange.Net.Clients } /// - /// Try get a client by type for the service collection + /// Get a list of the registered ISpotClient implementations /// /// public IEnumerable GetSpotClients() @@ -30,5 +36,12 @@ namespace CryptoExchange.Net.Clients return _serviceProvider.GetServices().ToList(); } + + /// + /// Get an ISpotClient implementation by exchange name + /// + /// + /// + public ISpotClient? SpotClient(string exchangeName) => _serviceProvider.GetServices()?.SingleOrDefault(s => s.ExchangeName.Equals(exchangeName, StringComparison.InvariantCultureIgnoreCase)); } } diff --git a/CryptoExchange.Net/Clients/CryptoSocketClient.cs b/CryptoExchange.Net/Clients/CryptoSocketClient.cs index 28af548..5c33ef9 100644 --- a/CryptoExchange.Net/Clients/CryptoSocketClient.cs +++ b/CryptoExchange.Net/Clients/CryptoSocketClient.cs @@ -1,16 +1,18 @@ 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 CryptoSocketClient : CryptoBaseClient, ICryptoSocketClient { + /// + /// ctor + /// + public CryptoSocketClient() + { + } + /// /// ctor /// diff --git a/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs b/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs index 732eee6..5f4928b 100644 --- a/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs +++ b/CryptoExchange.Net/Interfaces/ICryptoRestClient.cs @@ -16,6 +16,14 @@ namespace CryptoExchange.Net.Interfaces /// /// IEnumerable GetSpotClients(); + + /// + /// Get an ISpotClient implementation by exchange name + /// + /// + /// + ISpotClient? SpotClient(string exchangeName); + /// /// Try get ///