Permalink
Browse files

Add generic db constructor, and add optional mapper to all

  • Loading branch information...
asherber authored and pleb committed Dec 13, 2018
1 parent 9370bf9 commit cf0897272ef857a8e8a8e68ac69ba35aa73a1c5d
Showing with 31 additions and 10 deletions.
  1. +4 −1 PetaPoco.Tests.Unit/DatabaseTests.cs
  2. +27 −9 PetaPoco/Database.cs
@@ -11,7 +11,9 @@
using System.Dynamic;
using Moq;
using PetaPoco.Core;
using PetaPoco.Providers;
using PetaPoco.Tests.Unit.Models;
using PetaPoco.Utilities;
using Shouldly;
using Xunit;

@@ -95,6 +97,7 @@ public void Construct_GivenInvalidArguments_ShouldThrow()
throw;
}
});
Should.Throw<ArgumentException>(() => new Database<SqlServerDatabaseProvider>(null));
}

[Fact]
@@ -252,6 +255,6 @@ public object GetService(Type serviceType)

return null;
}
}
}
}
}
@@ -72,26 +72,28 @@ internal void DoPreExecute(IDbCommand cmd)
/// <summary>
/// Constructs an instance using the first connection string found in the app/web configuration file.
/// </summary>
/// <param name="defaultMapper">The default mapper to use when no specific mapper has been registered.</param>
/// <exception cref="InvalidOperationException">Thrown when no connection strings can registered.</exception>
public Database()
public Database(IMapper defaultMapper = null)
{
if (ConfigurationManager.ConnectionStrings.Count == 0)
throw new InvalidOperationException("One or more connection strings must be registered to use the no paramater constructor");

var entry = ConfigurationManager.ConnectionStrings[0];
_connectionString = entry.ConnectionString;
var providerName = !string.IsNullOrEmpty(entry.ProviderName) ? entry.ProviderName : "System.Data.SqlClient";
Initialise(DatabaseProvider.Resolve(providerName, false, _connectionString), null);
Initialise(DatabaseProvider.Resolve(providerName, false, _connectionString), defaultMapper);
}

/// <summary>
/// Constructs an instance using a supplied connection string name. The actual connection string and provider will be
/// read from app/web.config.
/// </summary>
/// <param name="connectionStringName">The name of the connection.</param>
/// <param name="defaultMapper">The default mapper to use when no specific mapper has been registered.</param>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionStringName" /> is null or empty.</exception>
/// <exception cref="InvalidOperationException">Thrown when a connection string cannot be found.</exception>
public Database(string connectionStringName)
public Database(string connectionStringName, IMapper defaultMapper = null)
{
if (string.IsNullOrEmpty(connectionStringName))
throw new ArgumentException("Connection string name must not be null or empty", nameof(connectionStringName));
@@ -103,7 +105,7 @@ public Database(string connectionStringName)

_connectionString = entry.ConnectionString;
var providerName = !string.IsNullOrEmpty(entry.ProviderName) ? entry.ProviderName : "System.Data.SqlClient";
Initialise(DatabaseProvider.Resolve(providerName, false, _connectionString), null);
Initialise(DatabaseProvider.Resolve(providerName, false, _connectionString), defaultMapper);
}
#endif

@@ -129,31 +131,33 @@ public Database(IDbConnection connection, IMapper defaultMapper = null)
}

/// <summary>
/// Constructs an instance using a supplied connections string and provider name.
/// Constructs an instance using a supplied connection string and provider name.
/// </summary>
/// <param name="connectionString">The database connection string.</param>
/// <param name="providerName">The database provider name.</param>
/// <param name="defaultMapper">The default mapper to use when no specific mapper has been registered.</param>
/// <remarks>
/// PetaPoco will automatically close and dispose any connections it creates.
/// </remarks>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString" /> is null or empty.</exception>
public Database(string connectionString, string providerName)
public Database(string connectionString, string providerName, IMapper defaultMapper = null)
{
if (string.IsNullOrEmpty(connectionString))
throw new ArgumentException("Connection string cannot be null or empty", nameof(connectionString));

_connectionString = connectionString;
Initialise(DatabaseProvider.Resolve(providerName, true, _connectionString), null);
Initialise(DatabaseProvider.Resolve(providerName, true, _connectionString), defaultMapper);
}

/// <summary>
/// Constructs an instance using the supplied connection string and DbProviderFactory.
/// </summary>
/// <param name="connectionString">The database connection string.</param>
/// <param name="factory">The DbProviderFactory to use for instantiating IDbConnection's.</param>
/// <param name="defaultMapper">The default mapper to use when no specific mapper has been registered.</param>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString" /> is null or empty.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="factory" /> is null.</exception>
public Database(string connectionString, DbProviderFactory factory)
public Database(string connectionString, DbProviderFactory factory, IMapper defaultMapper = null)
{
if (string.IsNullOrEmpty(connectionString))
throw new ArgumentException("Connection string must not be null or empty", nameof(connectionString));
@@ -162,7 +166,7 @@ public Database(string connectionString, DbProviderFactory factory)
throw new ArgumentNullException(nameof(factory));

_connectionString = connectionString;
Initialise(DatabaseProvider.Resolve(DatabaseProvider.Unwrap(factory).GetType(), false, _connectionString), null);
Initialise(DatabaseProvider.Resolve(DatabaseProvider.Unwrap(factory).GetType(), false, _connectionString), defaultMapper);
}

/// <summary>
@@ -2861,4 +2865,18 @@ public string FormatCommand(string sql, object[] args)
public event EventHandler<ExceptionEventArgs> ExceptionThrown;
#endregion
}

public class Database<TDatabaseProvider> : Database where TDatabaseProvider : IProvider
{
/// <summary>
/// Constructs an instance using a supplied connection string and provider type.
/// </summary>
/// <param name="connectionString">The database connection string.</param>
/// <param name="defaultMapper">The default mapper to use when no specific mapper has been registered.</param>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString" /> is null or empty.</exception>
public Database(string connectionString, IMapper defaultMapper = null)
: base(connectionString, typeof(TDatabaseProvider).Name, defaultMapper)
{
}
}
}

0 comments on commit cf08972

Please sign in to comment.