Skip to content

Commit f1eec4f

Browse files
Add guard to public APIs.
1 parent fe3c796 commit f1eec4f

18 files changed

Lines changed: 295 additions & 11 deletions

PosInformatique.Testing.Databases.sln

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing.Databases.SqlServer
6363
EndProject
6464
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing.Databases.SqlServer.SqlCmd.Tests", "tests\Testing.Databases.SqlServer.SqlCmd.Tests\Testing.Databases.SqlServer.SqlCmd.Tests.csproj", "{F8E025D7-4E2F-437A-ABFA-C43A1368E15A}"
6565
EndProject
66+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Testing.Databases.SqlServer.Shared", "src\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.shproj", "{B9F8C52D-4652-4FC6-A695-DC2F61BA05C9}"
67+
EndProject
6668
Global
6769
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6870
Debug|Any CPU = Debug|Any CPU
@@ -133,4 +135,10 @@ Global
133135
GlobalSection(ExtensibilityGlobals) = postSolution
134136
SolutionGuid = {FAC64573-D665-48A8-AC75-7B82B692EC9E}
135137
EndGlobalSection
138+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
139+
src\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems*{157ddf0d-9410-4646-94b9-9cee4c140f5e}*SharedItemsImports = 5
140+
src\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems*{8be60460-eba5-43de-b85d-c756e2988dc8}*SharedItemsImports = 5
141+
src\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems*{b9f8c52d-4652-4fc6-a695-dc2f61ba05c9}*SharedItemsImports = 13
142+
src\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems*{d3004122-ccdd-4ead-bd9e-da6dff470943}*SharedItemsImports = 5
143+
EndGlobalSection
136144
EndGlobal

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,19 @@ For Entity Framework migration:
177177
## 📦 NuGet package dependency versions
178178

179179
These tools rely on a minimal set of NuGet dependencies to ensure broad compatibility.
180-
They are built for **.NET 6.0** but also work seamlessly with newer versions of .NET:
181-
182-
- .NET 7.0
183-
- .NET 8.0
184-
- .NET 9.0
185-
- .NET 10.0
180+
They are built for **.NET Core 6.0** and **.NET Framework 4.6.2** but also work seamlessly with newer versions of .NET:
181+
182+
- .NET Framework 4.6.2
183+
- .NET Framework 4.7
184+
- .NET Framework 4.7.1
185+
- .NET Framework 4.7.2
186+
- .NET Framework 4.8
187+
- .NET Framework 4.8.1
188+
- .NET Core 6.0
189+
- .NET Core 7.0
190+
- .NET Core 8.0
191+
- .NET Core 9.0
192+
- .NET Core 10.0
186193

187194
### Dependency versions
188195

src/Testing.Databases.SqlServer.Dac/SqlServerDacDatabaseInitializer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@ public static class SqlServerDacDatabaseInitializer
2424
/// <param name="packageName">Full path of the DACPAC file.</param>
2525
/// <param name="connectionString">Connection string to the SQL Server with administrator rights.</param>
2626
/// <param name="settings">Additionnal settings for the DACPAC <paramref name="packageName"/> to deploy.</param>
27+
/// <exception cref="ArgumentNullException">If the specified <paramref name="initializer"/> argument is <see langword="null"/>.</exception>
28+
/// <exception cref="ArgumentNullException">If the specified <paramref name="packageName"/> argument is <see langword="null"/>.</exception>
29+
/// <exception cref="ArgumentNullException">If the specified <paramref name="connectionString"/> argument is <see langword="null"/>.</exception>
30+
/// <exception cref="FileNotFoundException">If no file exists with the specified <paramref name="packageName"/> argument.</exception>
2731
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which allows to perform query to initialize the data.</returns>
2832
public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, string packageName, string connectionString, SqlServerDacDeploymentSettings? settings = null)
2933
{
34+
Guard.ThrowIfNull(initializer, nameof(initializer));
35+
Guard.ThrowIfNull(packageName, nameof(packageName));
36+
Guard.ThrowIfNull(connectionString, nameof(connectionString));
37+
38+
if (!File.Exists(packageName))
39+
{
40+
throw new FileNotFoundException($"Could not find file '{packageName}'", packageName);
41+
}
42+
3043
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
3144

3245
var server = new SqlServer(connectionString);

src/Testing.Databases.SqlServer.Dac/SqlServerDacExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,22 @@ public static class SqlServerDacExtensions
2222
/// <param name="fileName">File name (including the path) for the DACPAC file to deploy.</param>
2323
/// <param name="databaseName">Name of the database which will be created.</param>
2424
/// <param name="settings">Additional settings of the database to deploy.</param>
25+
/// <exception cref="ArgumentNullException">If the specified <paramref name="server"/> argument is <see langword="null"/>.</exception>
26+
/// <exception cref="ArgumentNullException">If the specified <paramref name="fileName"/> argument is <see langword="null"/>.</exception>
27+
/// <exception cref="ArgumentNullException">If the specified <paramref name="databaseName"/> argument is <see langword="null"/>.</exception>
28+
/// <exception cref="FileNotFoundException">If no file exists with the specified <paramref name="fileName"/> argument.</exception>
2529
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which represents the deployed database.</returns>
2630
public static SqlServerDatabase DeployDacPackage(this SqlServer server, string fileName, string databaseName, SqlServerDacDeploymentSettings? settings = null)
2731
{
32+
Guard.ThrowIfNull(server, nameof(server));
33+
Guard.ThrowIfNull(fileName, nameof(fileName));
34+
Guard.ThrowIfNull(databaseName, nameof(databaseName));
35+
36+
if (!File.Exists(fileName))
37+
{
38+
throw new FileNotFoundException($"Could not find file '{fileName}'", fileName);
39+
}
40+
2841
using (var package = DacPackage.Load(fileName))
2942
{
3043
// Currently DacFx does not support to define explicitly the location of the database files.

src/Testing.Databases.SqlServer.Dac/Testing.Databases.SqlServer.Dac.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
<ProjectReference Include="..\Testing.Databases.SqlServer\Testing.Databases.SqlServer.csproj" />
2525
</ItemGroup>
2626

27+
<Import Project="..\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems" Label="Shared" />
28+
2729
</Project>

src/Testing.Databases.SqlServer.EntityFramework/EntityFrameworkDatabaseInitializerExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ public static class EntityFrameworkDatabaseInitializerExtensions
2121
/// <param name="initializer"><see cref="SqlServerDatabaseInitializer"/> which the initialization will be perform on.</param>
2222
/// <param name="context">Instance of the <see cref="DbContext"/> which represents the database schema to initialize.</param>
2323
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which allows to perform query to initialize the data.</returns>
24+
/// <exception cref="ArgumentNullException">If the specified <paramref name="initializer"/> argument is <see langword="null"/>.</exception>
25+
/// <exception cref="ArgumentNullException">If the specified <paramref name="context"/> argument is <see langword="null"/>.</exception>
2426
public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, DbContext context)
2527
{
28+
Guard.ThrowIfNull(initializer, nameof(initializer));
29+
Guard.ThrowIfNull(context, nameof(context));
30+
2631
var connectionString = context.Database.GetConnectionString();
2732

2833
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

src/Testing.Databases.SqlServer.EntityFramework/EntityFrameworkSqlServerExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ public static class EntityFrameworkSqlServerExtensions
2121
/// <param name="server"><see cref="SqlServer"/> instance where the database will be created.</param>
2222
/// <param name="name">Name of the database to create.</param>
2323
/// <param name="context"><see cref="DbContext"/> which represents the database to create.</param>
24+
/// <exception cref="ArgumentNullException">If the specified <paramref name="server"/> argument is <see langword="null"/>.</exception>
25+
/// <exception cref="ArgumentNullException">If the specified <paramref name="name"/> argument is <see langword="null"/>.</exception>
26+
/// <exception cref="ArgumentNullException">If the specified <paramref name="context"/> argument is <see langword="null"/>.</exception>
2427
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which represents the deployed database.</returns>
2528
public static SqlServerDatabase CreateDatabase(this SqlServer server, string name, DbContext context)
2629
{
30+
Guard.ThrowIfNull(server, nameof(server));
31+
Guard.ThrowIfNull(name, nameof(name));
32+
Guard.ThrowIfNull(context, nameof(context));
33+
2734
var database = server.GetDatabase(name);
2835

2936
context.Database.SetConnectionString(database.ConnectionString);
@@ -41,9 +48,16 @@ public static SqlServerDatabase CreateDatabase(this SqlServer server, string nam
4148
/// <param name="server"><see cref="SqlServer"/> instance where the database will be created.</param>
4249
/// <param name="name">Name of the database to create.</param>
4350
/// <param name="context"><see cref="DbContext"/> which represents the database to create.</param>
51+
/// <exception cref="ArgumentNullException">If the specified <paramref name="server"/> argument is <see langword="null"/>.</exception>
52+
/// <exception cref="ArgumentNullException">If the specified <paramref name="name"/> argument is <see langword="null"/>.</exception>
53+
/// <exception cref="ArgumentNullException">If the specified <paramref name="context"/> argument is <see langword="null"/>.</exception>
4454
/// <returns>A <see cref="Task"/> which represents the asynchronous operation and contains an instance of the <see cref="SqlServerDatabase"/> that represents the deployed database.</returns>
4555
public static async Task<SqlServerDatabase> CreateDatabaseAsync(this SqlServer server, string name, DbContext context)
4656
{
57+
Guard.ThrowIfNull(server, nameof(server));
58+
Guard.ThrowIfNull(name, nameof(name));
59+
Guard.ThrowIfNull(context, nameof(context));
60+
4761
var database = server.GetDatabase(name);
4862

4963
context.Database.SetConnectionString(database.ConnectionString);

src/Testing.Databases.SqlServer.EntityFramework/Testing.Databases.SqlServer.EntityFramework.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
<ProjectReference Include="..\Testing.Databases.SqlServer\Testing.Databases.SqlServer.csproj" />
2525
</ItemGroup>
2626

27+
<Import Project="..\Testing.Databases.SqlServer.Shared\Testing.Databases.SqlServer.Shared.projitems" Label="Shared" />
28+
2729
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="Guard.cs" company="P.O.S Informatique">
3+
// Copyright (c) P.O.S Informatique. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace PosInformatique
8+
{
9+
internal static class Guard
10+
{
11+
public static void ThrowIfNull(object? value, string paramName)
12+
{
13+
if (value is null)
14+
{
15+
throw new ArgumentNullException(paramName);
16+
}
17+
}
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>b9f8c52d-4652-4fc6-a695-dc2f61ba05c9</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>Testing.Databases.SqlServer.Shared</Import_RootNamespace>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Guard.cs" />
13+
</ItemGroup>
14+
</Project>

0 commit comments

Comments
 (0)