Add arguments guard for TelemetryPublisherAzure

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-23 22:18:25 +02:00
parent 70f00ca39d
commit c216620747
2 changed files with 30 additions and 0 deletions

24
NucuCar.Domain/Guard.cs Normal file
View file

@ -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!");
}
}
}
}

View file

@ -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});
}