diff --git a/CryptoExchange.Net.UnitTests/OptionsTests.cs b/CryptoExchange.Net.UnitTests/OptionsTests.cs index c56ede9..8771d90 100644 --- a/CryptoExchange.Net.UnitTests/OptionsTests.cs +++ b/CryptoExchange.Net.UnitTests/OptionsTests.cs @@ -253,11 +253,7 @@ namespace CryptoExchange.Net.UnitTests /// /// Default options for the futures client /// - public static TestClientOptions Default { get; set; } = new TestClientOptions() - { - Api1Options = new RestApiClientOptions(), - Api2Options = new RestApiClientOptions() - }; + public static TestClientOptions Default { get; set; } = new TestClientOptions(); /// /// The default receive window for requests @@ -268,41 +264,32 @@ namespace CryptoExchange.Net.UnitTests public RestApiClientOptions Api1Options { get => _api1Options; - set => _api1Options.Copy(_api1Options, value); + set => _api1Options = new RestApiClientOptions(_api1Options, value); } private RestApiClientOptions _api2Options = new RestApiClientOptions("https://api2.test.com/"); public RestApiClientOptions Api2Options { get => _api2Options; - set => _api2Options.Copy(_api2Options, value); + set => _api2Options = new RestApiClientOptions(_api2Options, value); } /// /// ctor /// - public TestClientOptions() + public TestClientOptions(): this(Default) { - if (Default == null) - return; - - Copy(this, Default); } - /// - /// Copy the values of the def to the input - /// - /// - /// - /// - public new void Copy(T input, T def) where T : TestClientOptions + public TestClientOptions(TestClientOptions? baseOn): base(baseOn) { - base.Copy(input, def); + if (baseOn == null) + return; - input.ReceiveWindow = def.ReceiveWindow; + ReceiveWindow = baseOn.ReceiveWindow; - input.Api1Options = new RestApiClientOptions(def.Api1Options); - input.Api2Options = new RestApiClientOptions(def.Api2Options); + Api1Options = new RestApiClientOptions(baseOn.Api1Options, null); + Api2Options = new RestApiClientOptions(baseOn.Api2Options, null); } } } diff --git a/CryptoExchange.Net/Objects/Options.cs b/CryptoExchange.Net/Objects/Options.cs index 7dcfdcb..94cf6ec 100644 --- a/CryptoExchange.Net/Objects/Options.cs +++ b/CryptoExchange.Net/Objects/Options.cs @@ -30,16 +30,24 @@ namespace CryptoExchange.Net.Objects public bool OutputOriginalData { get; set; } = false; /// - /// Copy the values of the def to the input + /// ctor /// - /// - /// - /// - public void Copy(T input, T def) where T : BaseOptions + public BaseOptions(): this(null) { - input.LogLevel = def.LogLevel; - input.LogWriters = def.LogWriters.ToList(); - input.OutputOriginalData = def.OutputOriginalData; + } + + /// + /// ctor + /// + /// Copy options from these options to the new options + public BaseOptions(BaseOptions? baseOptions) + { + if (baseOptions == null) + return; + + LogLevel = baseOptions.LogLevel; + LogWriters = baseOptions.LogWriters.ToList(); + OutputOriginalData = baseOptions.OutputOriginalData; } /// @@ -65,17 +73,23 @@ namespace CryptoExchange.Net.Objects public ApiCredentials? ApiCredentials { get; set; } /// - /// Copy the values of the def to the input + /// ctor /// - /// - /// - /// - public new void Copy(T input, T def) where T : BaseClientOptions + public BaseClientOptions() : this(null) { - base.Copy(input, def); + } - input.Proxy = def.Proxy; - input.ApiCredentials = def.ApiCredentials?.Copy(); + /// + /// ctor + /// + /// Copy options from these options to the new options + public BaseClientOptions(BaseClientOptions? baseOptions) : base(baseOptions) + { + if (baseOptions == null) + return; + + Proxy = baseOptions.Proxy; + ApiCredentials = baseOptions.ApiCredentials?.Copy(); } /// @@ -101,17 +115,23 @@ namespace CryptoExchange.Net.Objects public HttpClient? HttpClient { get; set; } /// - /// Copy the values of the def to the input + /// ctor /// - /// - /// - /// - public new void Copy(T input, T def) where T : BaseRestClientOptions + public BaseRestClientOptions(): this(null) { - base.Copy(input, def); + } - input.HttpClient = def.HttpClient; - input.RequestTimeout = def.RequestTimeout; + /// + /// ctor + /// + /// Copy options from these options to the new options + public BaseRestClientOptions(BaseRestClientOptions? baseOptions): base(baseOptions) + { + if (baseOptions == null) + return; + + HttpClient = baseOptions.HttpClient; + RequestTimeout = baseOptions.RequestTimeout; } /// @@ -170,23 +190,29 @@ namespace CryptoExchange.Net.Objects public int? SocketSubscriptionsCombineTarget { get; set; } /// - /// Copy the values of the def to the input + /// ctor /// - /// - /// - /// - public new void Copy(T input, T def) where T : BaseSocketClientOptions + public BaseSocketClientOptions(): this(null) { - base.Copy(input, def); - - input.AutoReconnect = def.AutoReconnect; - input.ReconnectInterval = def.ReconnectInterval; - input.MaxReconnectTries = def.MaxReconnectTries; - input.MaxResubscribeTries = def.MaxResubscribeTries; - input.MaxConcurrentResubscriptionsPerSocket = def.MaxConcurrentResubscriptionsPerSocket; - input.SocketResponseTimeout = def.SocketResponseTimeout; - input.SocketNoDataTimeout = def.SocketNoDataTimeout; - input.SocketSubscriptionsCombineTarget = def.SocketSubscriptionsCombineTarget; + } + + /// + /// ctor + /// + /// Copy options from these options to the new options + public BaseSocketClientOptions(BaseSocketClientOptions? baseOptions): base(baseOptions) + { + if (baseOptions == null) + return; + + AutoReconnect = baseOptions.AutoReconnect; + ReconnectInterval = baseOptions.ReconnectInterval; + MaxReconnectTries = baseOptions.MaxReconnectTries; + MaxResubscribeTries = baseOptions.MaxResubscribeTries; + MaxConcurrentResubscriptionsPerSocket = baseOptions.MaxConcurrentResubscriptionsPerSocket; + SocketResponseTimeout = baseOptions.SocketResponseTimeout; + SocketNoDataTimeout = baseOptions.SocketNoDataTimeout; + SocketSubscriptionsCombineTarget = baseOptions.SocketSubscriptionsCombineTarget; } /// @@ -232,27 +258,15 @@ namespace CryptoExchange.Net.Objects /// /// ctor /// - /// Copy values for the provided options + /// Copy values for the provided options #pragma warning disable 8618 // Will always get filled by the provided options - public ApiClientOptions(ApiClientOptions baseOn) + public ApiClientOptions(ApiClientOptions baseOptions, RestApiClientOptions? newValues) { - Copy(this, baseOn); + BaseAddress = newValues?.BaseAddress ?? baseOptions.BaseAddress; + ApiCredentials = newValues?.ApiCredentials?.Copy() ?? baseOptions.ApiCredentials?.Copy(); } #pragma warning restore 8618 - /// - /// Copy the values of the def to the input - /// - /// - /// - /// - public void Copy(T input, T def) where T : ApiClientOptions - { - if (def.BaseAddress != null) - input.BaseAddress = def.BaseAddress; - input.ApiCredentials = def.ApiCredentials?.Copy(); - } - /// public override string ToString() { @@ -304,26 +318,12 @@ namespace CryptoExchange.Net.Objects /// ctor /// /// Copy values for the provided options - public RestApiClientOptions(RestApiClientOptions baseOn) + public RestApiClientOptions(RestApiClientOptions baseOn, RestApiClientOptions newValues): base(baseOn, newValues) { - Copy(this, baseOn); - } - - /// - /// Copy the values of the def to the input - /// - /// - /// - /// - public new void Copy(T input, T def) where T : RestApiClientOptions - { - base.Copy(input, def); - - if(def.RateLimiters != null) - input.RateLimiters = def.RateLimiters.ToList(); - input.RateLimitingBehaviour = def.RateLimitingBehaviour; - input.AutoTimestamp = def.AutoTimestamp; - input.TimestampRecalculationInterval = def.TimestampRecalculationInterval; + RateLimitingBehaviour = newValues?.RateLimitingBehaviour ?? baseOn.RateLimitingBehaviour; + AutoTimestamp = newValues?.AutoTimestamp ?? baseOn.AutoTimestamp; + TimestampRecalculationInterval = newValues?.TimestampRecalculationInterval ?? baseOn.TimestampRecalculationInterval; + RateLimiters = newValues?.RateLimiters.ToList() ?? baseOn?.RateLimiters.ToList() ?? new List(); } ///