-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database connection validation and reorganize project structure
The code adds a validation for database connections along with appropriate error handling. It also refactors the codebase structure including string extension methods that have been moved to a new folder. Furthermore, test project references have been updated and new configuration files added.
- Loading branch information
Gary Woodfine
committed
Mar 26, 2024
1 parent
93f0c32
commit 777bb1d
Showing
18 changed files
with
253 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<root> | ||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||
<xsd:element name="root" msdata:IsDataSet="true"> | ||
|
||
</xsd:element> | ||
</xsd:schema> | ||
<resheader name="resmimetype"> | ||
<value>text/microsoft-resx</value> | ||
</resheader> | ||
<resheader name="version"> | ||
<value>1.3</value> | ||
</resheader> | ||
<resheader name="reader"> | ||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<resheader name="writer"> | ||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
</resheader> | ||
<data name="DatabaseConnectionError" xml:space="preserve"> | ||
<value>The connection to the database failed. Review your connection string information and ensure it valid or that you have access to the database server. </value> | ||
</data> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using System; | ||
|
||
namespace Threenine.Database.Exceptions; | ||
|
||
public class DatabaseConfigurationException(string message) : Exception(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using System; | ||
|
||
namespace Threenine.Exceptions; | ||
|
||
public class DatabaseConnectionStringException(string message) : Exception(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Data.Common; | ||
using Threenine.Database.Exceptions; | ||
|
||
namespace Threenine.Database.Extensions; | ||
|
||
public static class ConnectionStringExtensions | ||
{ | ||
public static bool Validate(this string connectionString) | ||
{ | ||
try | ||
{ | ||
var factory = DbProviderFactories.GetFactory(connectionString); | ||
using var connection = factory.CreateConnection(); | ||
if (connection == null) | ||
throw new DatabaseConfigurationException(ConfigurationErrors.DatabaseConnectionError); | ||
|
||
connection.Open(); | ||
return true; | ||
} | ||
catch (Exception e) | ||
Check warning on line 21 in src/Extensions/ConnectionStringExtensions.cs GitHub Actions / build
Check warning on line 21 in src/Extensions/ConnectionStringExtensions.cs GitHub Actions / build
|
||
{ | ||
throw new DatabaseConfigurationException(ConfigurationErrors.DatabaseConnectionError); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Text; | ||
|
||
namespace Threenine.Database.Extensions; | ||
|
||
public static class StringExtensions | ||
{ | ||
public static string ToSnakeCase(this string text) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append(char.ToLowerInvariant(text[0])); | ||
for (var i = 1; i < text.Length; ++i) | ||
{ | ||
var c = text[i]; | ||
if (char.IsUpper(c)) | ||
{ | ||
sb.Append('_'); | ||
sb.Append(char.ToLowerInvariant(c)); | ||
} | ||
else | ||
{ | ||
sb.Append(c); | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Data.Common; | ||
using Threenine.Database; | ||
using Threenine.Database.Exceptions; | ||
using Threenine.Exceptions; | ||
|
||
namespace Threenine.Validators; | ||
|
||
public class ConnectionsStrings | ||
{ | ||
public bool Validate(string connectionString) | ||
{ | ||
try | ||
{ | ||
var factory = DbProviderFactories.GetFactory(connectionString); | ||
using var connection = factory.CreateConnection(); | ||
if (connection == null) | ||
throw new DatabaseConfigurationException(ConfigurationErrors.DatabaseConnectionError); | ||
|
||
connection.Open(); | ||
return true; | ||
} | ||
catch (Exception e) | ||
Check warning on line 23 in src/Validators/ConnectionsStrings.cs GitHub Actions / build
Check warning on line 23 in src/Validators/ConnectionsStrings.cs GitHub Actions / build
|
||
{ | ||
throw new DatabaseConfigurationException(ConfigurationErrors.DatabaseConnectionError); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))"/> | ||
<PropertyGroup> | ||
<IsPackable>False</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
<RootNamespace>Threenine.Database.Configuration.Tests</RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" /> | ||
<PackageReference Include="FluentValidation"/> | ||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions"/> | ||
<PackageReference Include="FluentAssertions"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
<PackageReference Include="NSubstitute"/> | ||
<PackageReference Include="xunit"/> | ||
<PackageReference Include="xunit.runner.visualstudio"/> | ||
<PackageReference Include="NBuilder"/> | ||
<PackageReference Include="Moq"/> | ||
<PackageReference Include="Shouldly"/> | ||
<PackageReference Include="coverlet.collector"/> | ||
<PackageReference Include="coverlet.msbuild"/> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
<Using Include="FluentAssertions"/> | ||
<Using Include="NSubstitute"/> | ||
<Using Include="Shouldly"/> | ||
|
||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.3" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" /> | ||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.3" /> | ||
<PackageVersion Include="Npgsql" Version="8.0.2" /> | ||
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" /> | ||
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.8.0" /> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageVersion Include="FluentValidation" Version="11.9.0" /> | ||
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" /> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageVersion Include="xunit" Version="2.6.6" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" /> | ||
<PackageVersion Include="NBuilder" Version="6.1.0" /> | ||
<PackageVersion Include="Moq" Version="4.20.70" /> | ||
<PackageVersion Include="Shouldly" Version="4.2.1" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageVersion Include="coverlet.msbuild" Version="6.0.0" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))"/> | ||
|
||
</Project> |
4 changes: 2 additions & 2 deletions
4
tests/Unit/StringExtensionTests.cs → ...s/Unit/Extensions/StringExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters