diff --git a/src/DataMasker.Examples/Program.cs b/src/DataMasker.Examples/Program.cs index 5835cb6..13194e6 100644 --- a/src/DataMasker.Examples/Program.cs +++ b/src/DataMasker.Examples/Program.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using DataMasker.Interfaces; using DataMasker.Models; +using DataMasker.Utils; using Newtonsoft.Json; using Newtonsoft.Json.Schema; using Newtonsoft.Json.Schema.Generation; @@ -32,9 +33,11 @@ public static void RunExample() Config config = LoadConfig(3); - var dataProviders = new List(); - dataProviders.Add(new BogusDataProvider(config.DataGeneration)); - dataProviders.Add(new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.Config.connectionString.ToString()))); + var dataProviders = new List + { + new BogusDataProvider(config.DataGeneration), + new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.GetConnectionString())) + }; //create a data masker IDataMasker dataMasker = new DataMasker(dataProviders); diff --git a/src/DataMasker.Runner/Program.cs b/src/DataMasker.Runner/Program.cs index 2c10f90..e253ec0 100644 --- a/src/DataMasker.Runner/Program.cs +++ b/src/DataMasker.Runner/Program.cs @@ -9,6 +9,7 @@ using DataMasker.Models; using Konsole; using Newtonsoft.Json; +using DataMasker.Utils; namespace DataMasker.Runner { @@ -128,9 +129,11 @@ private static void Execute( WriteLine("Masking Data"); UpdateProgress(ProgressType.Overall, 0, config.Tables.Count, "Overall Progress"); - var dataProviders = new List(); - dataProviders.Add(new BogusDataProvider(config.DataGeneration)); - dataProviders.Add(new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.Config.connectionString.ToString()))); + var dataProviders = new List + { + new BogusDataProvider(config.DataGeneration), + new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.GetConnectionString())) + }; //create a data masker IDataMasker dataMasker = new DataMasker(dataProviders); diff --git a/src/DataMasker/DataSources/SqlDataSource.cs b/src/DataMasker/DataSources/SqlDataSource.cs index 4f05d56..add7f54 100644 --- a/src/DataMasker/DataSources/SqlDataSource.cs +++ b/src/DataMasker/DataSources/SqlDataSource.cs @@ -28,19 +28,9 @@ public SqlDataSource( DataSourceConfig sourceConfig) { _sourceConfig = sourceConfig; - if (sourceConfig.Config.connectionString != null && !string.IsNullOrWhiteSpace(sourceConfig.Config.connectionString.ToString())) - { - _connectionString = sourceConfig.Config.connectionString; - } - else - { - _connectionString = - $"User ID={sourceConfig.Config.userName};Password={sourceConfig.Config.password};Data Source={sourceConfig.Config.server};Initial Catalog={sourceConfig.Config.name};Persist Security Info=False;"; - } - + _connectionString = sourceConfig.GetConnectionString(); } - /// /// Gets the data. /// diff --git a/src/DataMasker/Utils/Utils.cs b/src/DataMasker/Utils/Utils.cs index 70224ab..92cc798 100644 --- a/src/DataMasker/Utils/Utils.cs +++ b/src/DataMasker/Utils/Utils.cs @@ -1,4 +1,5 @@ using Bogus.DataSets; +using DataMasker.Models; using System.Collections.Generic; using System.Linq; @@ -59,6 +60,22 @@ internal static string MakeColumnNameSafe(string paramName) } return paramName; - } + } + + public static string GetConnectionString(this DataSourceConfig sourceConfig) + { + string connectionString; + if (sourceConfig.Config.connectionString != null && !string.IsNullOrWhiteSpace(sourceConfig.Config.connectionString.ToString())) + { + connectionString = sourceConfig.Config.connectionString; + } + else + { + connectionString = + $"User ID={sourceConfig.Config.userName};Password={sourceConfig.Config.password};Data Source={sourceConfig.Config.server};Initial Catalog={sourceConfig.Config.name};Persist Security Info=False;"; + } + + return connectionString; + } } } \ No newline at end of file