• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 26601445198

28 May 2026 08:50PM UTC coverage: 55.738% (+0.1%) from 55.595%
26601445198

push

github

pwelter34
Normalize enums and add enum data readers

1381 of 3245 branches covered (42.56%)

Branch coverage included in aggregate %.

38 of 58 new or added lines in 7 files covered. (65.52%)

3 existing lines in 1 file now uncovered.

4438 of 7195 relevant lines covered (61.68%)

311.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.86
/src/FluentCommand/DataParameterHandlers.cs
1
using System.Collections.Concurrent;
2
using System.Data.Common;
3

4
using FluentCommand.Extensions;
5
using FluentCommand.Handlers;
6

7
namespace FluentCommand;
8

9
/// <summary>
10
/// Provides a registry for associating .NET types with custom data parameter handlers used to set database parameter values and types.
11
/// </summary>
12
/// <remarks>
13
/// This class enables registration and retrieval of type-specific handlers that control how values are
14
/// assigned to database parameters, supporting extensibility for custom or non-standard data types. Handlers can be
15
/// added for new types at runtime. Thread-safe for concurrent access.
16
/// </remarks>
17
public static class DataParameterHandlers
18
{
19
    private static readonly ConcurrentDictionary<Type, IDataParameterHandler> _dataTypeHandlers;
20

21
    static DataParameterHandlers()
22
    {
23
        _dataTypeHandlers = new ConcurrentDictionary<Type, IDataParameterHandler>();
3✔
24
        _dataTypeHandlers.TryAdd(typeof(ConcurrencyToken), new ConcurrencyTokenHandler());
3✔
25

26
#if NET6_0_OR_GREATER
27
        // once ADO supports DateOnly & TimeOnly, this can be removed
28
        _dataTypeHandlers.TryAdd(typeof(DateOnly), new DateOnlyHandler());
3✔
29
        _dataTypeHandlers.TryAdd(typeof(TimeOnly), new TimeOnlyHandler());
3✔
30
#endif
31
    }
3✔
32

33
    /// <summary>
34
    /// Adds the data type handler.
35
    /// </summary>
36
    /// <typeparam name="THandler">The type of the handler.</typeparam>
37
    public static void AddTypeHandler<THandler>()
38
        where THandler : IDataParameterHandler, new()
39
    {
40
        var handler = new THandler();
×
41
        AddTypeHandler(handler);
×
42
    }
×
43

44
    /// <summary>
45
    /// Adds the data type handler.
46
    /// </summary>
47
    /// <typeparam name="THandler">The type of the handler.</typeparam>
48
    /// <param name="handler">The handler.</param>
49
    public static void AddTypeHandler<THandler>(THandler handler)
50
        where THandler : IDataParameterHandler
51
    {
52
        _dataTypeHandlers.TryAdd(handler.ValueType, handler);
×
53
    }
×
54

55
    /// <summary>
56
    /// Gets the type handler for the specified <paramref name="type"/>.
57
    /// </summary>
58
    /// <param name="type">The type to get a handler for.</param>
59
    /// <returns>The <see cref="IDataParameterHandler"/> for the specified type; otherwise null if not found</returns>
60
    public static IDataParameterHandler? GetTypeHandler(Type type)
61
    {
62
        var underlyingType = type.GetUnderlyingType();
553✔
63
        return _dataTypeHandlers.TryGetValue(underlyingType, out var handler) ? handler : null;
553✔
64
    }
65

66

67
    /// <summary>
68
    /// Sets the Value and DbType on the specified <paramref name="parameter"/> using the registered type handlers.
69
    /// </summary>
70
    /// <param name="parameter">The parameter to set the value on.</param>
71
    /// <param name="value">The value to set.</param>
72
    /// <param name="type">The data type to use.</param>
73
    /// <remarks>
74
    /// Enum and nullable enum values are converted to their numeric underlying type before mapping.
75
    /// </remarks>
76
    public static void SetValue(DbParameter parameter, object? value, Type type)
77
    {
78
        var valueType = type.GetUnderlyingType();
542✔
79
        if (valueType.IsEnum)
542!
80
        {
NEW
81
            valueType = Enum.GetUnderlyingType(valueType);
×
NEW
82
            if (value is not null && value != DBNull.Value)
×
NEW
83
                value = Convert.ChangeType(value, valueType);
×
84
        }
85

86
        var handler = GetTypeHandler(valueType);
542✔
87

88
        value ??= DBNull.Value;
542✔
89

90
        if (handler != null)
542✔
91
        {
92
            handler.SetValue(parameter, value);
4✔
93
            return;
4✔
94
        }
95

96
        parameter.Value = value;
538✔
97
        parameter.DbType = valueType.ToDbType();
538✔
98
    }
538✔
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc