1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-11 01:46:12 +00:00
This commit is contained in:
JKorf 2024-02-02 16:55:28 +01:00
parent acd9b0d533
commit 8a869e8e1d
2 changed files with 59 additions and 0 deletions

View File

@ -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<Type, object?> _serviceCache = new Dictionary<Type, object?>();
private readonly IServiceProvider _serviceProvider;
public CryptoExchangeClient(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_serviceCache = new Dictionary<Type, object?>();
}
public IEnumerable<ISpotClient> GetSpotClients()
{
return _serviceProvider.GetServices<ISpotClient>().ToList();
}
public T? TryGet<T>()
{
var type = typeof(T);
if (_serviceCache.TryGetValue(type, out var value))
return (T?)value;
var result = _serviceProvider.GetService<T>();
_serviceCache.Add(type, result);
return result;
}
public void Dispose()
{
_serviceCache.Clear();
}
}
}

View File

@ -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<ISpotClient> GetSpotClients();
T? TryGet<T>();
}
}