NucuCar/NucuCar.Core/Utilities/ConnectionStringParser.cs

41 lines
1.5 KiB
C#
Raw Normal View History

2019-12-18 20:57:33 +00:00
using System;
using System.Collections.Generic;
namespace NucuCar.Core.Utilities
2019-12-18 20:57:33 +00:00
{
/// <summary>
/// ConnectionStringParser is an utility service to parse and validate connection strings.
/// </summary>
public static class ConnectionStringParser
{
/// <summary>
/// Parse parses and validates a provided connection string.
/// </summary>
/// <param name="connectionString">The connection string to parse</param>
/// <returns>A dictionary object containing the values of the connection string.</returns>
/// <exception cref="ArgumentException"></exception>
public static Dictionary<string, string> Parse(string connectionString)
{
2019-12-19 20:50:51 +00:00
if (connectionString.Equals(string.Empty))
{
throw new ArgumentException("ConnectionString can't be empty!");
}
2019-12-18 20:57:33 +00:00
var items = connectionString.Split(";");
var parsedConnectionString = new Dictionary<string, string>();
foreach (var item in items)
{
var keyValue = item.Split("=", 2);
if (keyValue.Length < 2)
2019-12-18 20:57:33 +00:00
{
throw new ArgumentException(
$"Invalid argument for connection string, expected KEY=VALUE got {item}");
}
2019-12-19 20:50:51 +00:00
parsedConnectionString.Add(keyValue[0], keyValue[1]);
2019-12-18 20:57:33 +00:00
}
return parsedConnectionString;
}
}
}