From c2166207472f1e9c40dbd1037b6f38aca008c416 Mon Sep 17 00:00:00 2001 From: Denis NUTIU Date: Sat, 23 Nov 2019 22:18:25 +0200 Subject: [PATCH] Add arguments guard for TelemetryPublisherAzure --- NucuCar.Domain/Guard.cs | 24 +++++++++++++++++++ .../Telemetry/TelemetryPublisherAzure.cs | 6 +++++ 2 files changed, 30 insertions(+) create mode 100644 NucuCar.Domain/Guard.cs diff --git a/NucuCar.Domain/Guard.cs b/NucuCar.Domain/Guard.cs new file mode 100644 index 0000000..10bfa75 --- /dev/null +++ b/NucuCar.Domain/Guard.cs @@ -0,0 +1,24 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace NucuCar.Domain +{ + public static class Guard + { + internal static void ArgumentNotNullOrWhiteSpace(string argumentName, string argument) + { + if (string.IsNullOrWhiteSpace(argument)) + { + throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); + } + } + + public static void ArgumentNotNull(string argumentName, object argument) + { + if (argument == null) + { + throw new ArgumentNullException($"The argument {argumentName} is null or whitespace!"); + } + } + } +} \ No newline at end of file diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs index 13817d9..d8b0419 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherAzure.cs @@ -30,6 +30,7 @@ namespace NucuCar.Domain.Telemetry public static TelemetryPublisher CreateFromConnectionString(string connectionString) { + Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() {ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"}); } @@ -37,6 +38,8 @@ namespace NucuCar.Domain.Telemetry public static TelemetryPublisher CreateFromConnectionString(string connectionString, string telemetrySource) { + Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); + Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() {ConnectionString = connectionString, TelemetrySource = telemetrySource}); } @@ -44,6 +47,9 @@ namespace NucuCar.Domain.Telemetry public static TelemetryPublisher CreateFromConnectionString(string connectionString, string telemetrySource, ILogger logger) { + Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString); + Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource); + Guard.ArgumentNotNull(nameof(logger), logger); return new TelemetryPublisherAzure(new TelemetryPublisherBuilderOptions() {ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger}); }