Skip to content

Commit

Permalink
logging and documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Oct 12, 2023
1 parent 4a9f55e commit c1b0e1d
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/FluentCommand/ConcurrencyToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@

namespace FluentCommand;

/// <summary>
/// A structure to hold concurrency token
/// </summary>
public readonly struct ConcurrencyToken : IEquatable<ConcurrencyToken>
{
/// <summary>
/// The default empty token
/// </summary>
public static readonly ConcurrencyToken None = new(Array.Empty<byte>());

/// <summary>
/// Gets the underlying value of the token.
/// </summary>
/// <value>
/// The underlying value of the token.
/// </value>
public byte[] Value { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ConcurrencyToken"/> struct.
/// </summary>
/// <param name="value">The value.</param>
public ConcurrencyToken(byte[] value)
{
Value = value ?? Array.Empty<byte>();
}

/// <summary>
/// Initializes a new instance of the <see cref="ConcurrencyToken"/> struct.
/// </summary>
/// <param name="value">The value.</param>
public ConcurrencyToken(string value)
{
#if NET5_0_OR_GREATER
Expand All @@ -22,6 +42,7 @@ public ConcurrencyToken(string value)
#endif
}

/// <inheritdoc />
public override string ToString()
{
#if NET5_0_OR_GREATER
Expand All @@ -31,16 +52,19 @@ public override string ToString()
#endif
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is ConcurrencyToken token && Equals(token);
}

/// <inheritdoc />
public bool Equals(ConcurrencyToken other)
{
return EqualityComparer<byte[]>.Default.Equals(Value, other.Value);
}

/// <inheritdoc />
public override int GetHashCode()
{
return Value.GetHashCode();
Expand Down
150 changes: 150 additions & 0 deletions src/FluentCommand/DataConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace FluentCommand;

/// <summary>
/// A configuration builder class
/// </summary>
public class DataConfigurationBuilder
{
private readonly IServiceCollection _services;
Expand All @@ -19,25 +22,52 @@ public class DataConfigurationBuilder
private Type _queryGeneratorType;
private Type _queryLoggerType;

/// <summary>
/// Initializes a new instance of the <see cref="DataConfigurationBuilder"/> class.
/// </summary>
/// <param name="services">The services.</param>
public DataConfigurationBuilder(IServiceCollection services)
{
_services = services;
}


/// <summary>
/// The name of the connection to resolve the connection string from configuration.
/// </summary>
/// <param name="connectionName">Name of the connection.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder UseConnectionName(string connectionName)
{
_connectionName = connectionName;
return this;
}

/// <summary>
/// The connection string to use with fluent command.
/// </summary>
/// <param name="connectionString">The connection string.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder UseConnectionString(string connectionString)
{
_connectionString = connectionString;
return this;
}


/// <summary>
/// Adds the provider factory to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="providerFactory">The provider factory.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="DbProviderFactory"/>
public DataConfigurationBuilder AddProviderFactory<TService>(TService providerFactory)
where TService : DbProviderFactory
{
Expand All @@ -46,6 +76,15 @@ public DataConfigurationBuilder AddProviderFactory<TService>(TService providerFa
return this;
}

/// <summary>
/// Adds the provider factory to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="implementationFactory">The implementation factory.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="DbProviderFactory"/>
public DataConfigurationBuilder AddProviderFactory<TService>(Func<IServiceProvider, TService> implementationFactory)
where TService : DbProviderFactory
{
Expand All @@ -54,6 +93,14 @@ public DataConfigurationBuilder AddProviderFactory<TService>(Func<IServiceProvid
return this;
}

/// <summary>
/// Adds the provider factory to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="DbProviderFactory"/>
public DataConfigurationBuilder AddProviderFactory<TService>()
where TService : DbProviderFactory
{
Expand All @@ -63,6 +110,15 @@ public DataConfigurationBuilder AddProviderFactory<TService>()
}


/// <summary>
/// Adds the data cache service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="dataCache">The data cache.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataCache"/>
public DataConfigurationBuilder AddDataCache<TService>(TService dataCache)
where TService : class, IDataCache
{
Expand All @@ -71,6 +127,15 @@ public DataConfigurationBuilder AddDataCache<TService>(TService dataCache)
return this;
}

/// <summary>
/// Adds the data cache service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="implementationFactory">The implementation factory.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataCache"/>
public DataConfigurationBuilder AddDataCache<TService>(Func<IServiceProvider, TService> implementationFactory)
where TService : class, IDataCache
{
Expand All @@ -79,6 +144,14 @@ public DataConfigurationBuilder AddDataCache<TService>(Func<IServiceProvider, TS
return this;
}

/// <summary>
/// Adds the data cache service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataCache"/>
public DataConfigurationBuilder AddDataCache<TService>()
where TService : class, IDataCache
{
Expand All @@ -88,6 +161,15 @@ public DataConfigurationBuilder AddDataCache<TService>()
}


/// <summary>
/// Adds the query generator service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="queryGenerator">The query generator.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IQueryGenerator"/>
public DataConfigurationBuilder AddQueryGenerator<TService>(TService queryGenerator)
where TService : class, IQueryGenerator
{
Expand All @@ -96,6 +178,14 @@ public DataConfigurationBuilder AddQueryGenerator<TService>(TService queryGenera
return this;
}

/// <summary>
/// Adds the query generator service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IQueryGenerator"/>
public DataConfigurationBuilder AddQueryGenerator<TService>()
where TService : class, IQueryGenerator
{
Expand All @@ -104,6 +194,15 @@ public DataConfigurationBuilder AddQueryGenerator<TService>()
return this;
}

/// <summary>
/// Adds the query generator service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="implementationFactory">The implementation factory.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IQueryGenerator"/>
public DataConfigurationBuilder AddQueryGenerator<TService>(Func<IServiceProvider, TService> implementationFactory)
where TService : class, IQueryGenerator
{
Expand All @@ -112,25 +211,52 @@ public DataConfigurationBuilder AddQueryGenerator<TService>(Func<IServiceProvide
return this;
}

/// <summary>
/// Adds the SQL server generator to use with this configuration.
/// </summary>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder AddSqlServerGenerator()
{
AddQueryGenerator<SqlServerGenerator>();
return this;
}

/// <summary>
/// Adds the sqlite generator to use with this configuration.
/// </summary>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder AddSqliteGenerator()
{
AddQueryGenerator<SqliteGenerator>();
return this;
}

/// <summary>
/// Adds the PostgreSQL generator to use with this configuration.
/// </summary>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder AddPostgreSqlGenerator()
{
AddQueryGenerator<PostgreSqlGenerator>();
return this;
}


/// <summary>
/// Adds the query logger service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="queryLogger">The query logger.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataQueryLogger"/>
public DataConfigurationBuilder AddQueryLogger<TService>(TService queryLogger)
where TService : class, IDataQueryLogger
{
Expand All @@ -139,6 +265,14 @@ public DataConfigurationBuilder AddQueryLogger<TService>(TService queryLogger)
return this;
}

/// <summary>
/// Adds the query logger service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataQueryLogger"/>
public DataConfigurationBuilder AddQueryLogger<TService>()
where TService : class, IDataQueryLogger
{
Expand All @@ -147,6 +281,15 @@ public DataConfigurationBuilder AddQueryLogger<TService>()
return this;
}

/// <summary>
/// Adds the query logger service to use with this configuration.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="implementationFactory">The implementation factory.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
/// <seealso cref="IDataQueryLogger"/>
public DataConfigurationBuilder AddQueryLogger<TService>(Func<IServiceProvider, TService> implementationFactory)
where TService : class, IDataQueryLogger
{
Expand All @@ -156,6 +299,13 @@ public DataConfigurationBuilder AddQueryLogger<TService>(Func<IServiceProvider,
}


/// <summary>
/// Adds services via the configuration setup action.
/// </summary>
/// <param name="setupAction">The configuration setup action.</param>
/// <returns>
/// The same configuration builder so that multiple calls can be chained.
/// </returns>
public DataConfigurationBuilder AddService(Action<IServiceCollection> setupAction)
{
setupAction(_services);
Expand Down
13 changes: 11 additions & 2 deletions src/FluentCommand/DataQueryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace FluentCommand;
/// A class to log queries to string delegate
/// </summary>
/// <seealso cref="FluentCommand.IDataQueryLogger" />
public class DataQueryLogger : IDataQueryLogger
public partial class DataQueryLogger : IDataQueryLogger
{
private readonly ILogger<DataQueryLogger> _logger;
private readonly IDataQueryFormatter _formatter;
Expand Down Expand Up @@ -44,6 +44,15 @@ public virtual void LogCommand(IDbCommand command, TimeSpan duration, Exception

var output = _formatter.FormatCommand(command, duration, exception);

_logger.LogInformation(exception, output);
if (exception == null)
LogCommand(output);
else
LogError(output, exception);
}

[LoggerMessage(0, LogLevel.Debug, "{output}")]
public partial void LogCommand(string output);

[LoggerMessage(1, LogLevel.Error, "{output}")]
public partial void LogError(string output, Exception exception);
}
Loading

0 comments on commit c1b0e1d

Please sign in to comment.