Skip to content

Commit 52364af

Browse files
committed
Database accessor generation is now more deterministic and doesn't rely on generic type overloads
1 parent 6b7f2ff commit 52364af

37 files changed

Lines changed: 223 additions & 1324 deletions

src/Dibix.Dapper/DapperDatabaseAccessor.cs

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public DapperDatabaseAccessor(DbConnection connection = null, IDbTransaction def
2727
#endregion
2828

2929
#region Overrides
30-
protected override int Execute(string commandText, CommandType commandType, int? commandTimeout, ParametersVisitor parameters)
30+
protected override int Execute(string commandText, CommandType commandType, ParametersVisitor parameters, int? commandTimeout)
3131
{
3232
return base.Connection.Execute(commandText, CollectParameters(parameters), _defaultTransaction, commandTimeout ?? _defaultCommandTimeout, commandType);
3333
}
3434

35-
protected override Task<int> ExecuteAsync(string commandText, CommandType commandType, int? commandTimeout, ParametersVisitor parameters, CancellationToken cancellationToken)
35+
protected override Task<int> ExecuteAsync(string commandText, CommandType commandType, ParametersVisitor parameters, int? commandTimeout, CancellationToken cancellationToken)
3636
{
3737
CommandDefinition command = new CommandDefinition(commandText, CollectParameters(parameters), _defaultTransaction, commandTimeout ?? _defaultCommandTimeout, commandType, cancellationToken: cancellationToken);
3838
return base.Connection.ExecuteAsync(command);
@@ -52,64 +52,10 @@ protected override Task<IEnumerable<T>> QueryManyAsync<T>(string commandText, Co
5252
return base.Connection.QueryAsync<T>(command);
5353
}
5454

55-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TReturn> map, string splitOn)
55+
protected override IEnumerable<TReturn> QueryMany<TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Type[] types, Func<object[], TReturn> map, string splitOn)
5656
{
57-
DecoratedTypeMap.Adapt<TFirst, TSecond>();
58-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
59-
}
60-
61-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TReturn> map, string splitOn)
62-
{
63-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird>();
64-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
65-
}
66-
67-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, string splitOn)
68-
{
69-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth>();
70-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
71-
}
72-
73-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, string splitOn)
74-
{
75-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth>();
76-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
77-
}
78-
79-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn> map, string splitOn)
80-
{
81-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>();
82-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
83-
}
84-
85-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map, string splitOn)
86-
{
87-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>();
88-
return base.Connection.Query(commandText, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
89-
}
90-
91-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TReturn> map, string splitOn)
92-
{
93-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth>();
94-
Type[] types = { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth) };
95-
Func<object[], TReturn> mapWrapper = x => map((TFirst)x[0], (TSecond)x[1], (TThird)x[2], (TFourth)x[3], (TFifth)x[4], (TSixth)x[5], (TSeventh)x[6], (TEighth)x[7]);
96-
return base.Connection.Query(commandText, types, mapWrapper, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
97-
}
98-
99-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TReturn> map, string splitOn)
100-
{
101-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth>();
102-
Type[] types = { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth) };
103-
Func<object[], TReturn> mapWrapper = x => map((TFirst)x[0], (TSecond)x[1], (TThird)x[2], (TFourth)x[3], (TFifth)x[4], (TSixth)x[5], (TSeventh)x[6], (TEighth)x[7], (TNinth)x[8]);
104-
return base.Connection.Query(commandText, types, mapWrapper, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
105-
}
106-
107-
protected override IEnumerable<TReturn> QueryMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TReturn>(string commandText, CommandType commandType, ParametersVisitor parameters, Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh, TReturn> map, string splitOn)
108-
{
109-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh>();
110-
Type[] types = { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth), typeof(TTenth), typeof(TEleventh) };
111-
Func<object[], TReturn> mapWrapper = x => map((TFirst)x[0], (TSecond)x[1], (TThird)x[2], (TFourth)x[3], (TFifth)x[4], (TSixth)x[5], (TSeventh)x[6], (TEighth)x[7], (TNinth)x[8], (TTenth)x[9], (TEleventh)x[10]);
112-
return base.Connection.Query(commandText, types, mapWrapper, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
57+
DecoratedTypeMap.Adapt(types);
58+
return base.Connection.Query(commandText, types, map, CollectParameters(parameters), _defaultTransaction, commandType: commandType, commandTimeout: _defaultCommandTimeout, splitOn: splitOn);
11359
}
11460

11561
protected override T QuerySingle<T>(string commandText, CommandType commandType, ParametersVisitor parameters)

src/Dibix.Dapper/DapperGridResultReader.cs

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,104 +14,64 @@ internal sealed class DapperGridResultReader : MultipleResultReader, IMultipleRe
1414
#endregion
1515

1616
#region Properties
17-
public override bool IsConsumed => this._reader.IsConsumed;
17+
public override bool IsConsumed => _reader.IsConsumed;
1818
#endregion
1919

2020
#region Constructor
2121
public DapperGridResultReader(SqlMapper.GridReader reader, string commandText, CommandType commandType, ParametersVisitor parameters) : base(commandText, commandType, parameters, isSqlClient: reader.Command is SqlCommand)
2222
{
23-
this._reader = reader;
23+
_reader = reader;
2424
}
2525
#endregion
2626

2727
#region IMultipleResultReader Members
2828
protected override IEnumerable<T> ReadMany<T>()
2929
{
3030
DecoratedTypeMap.Adapt<T>();
31-
return this._reader.Read<T>();
31+
return _reader.Read<T>();
3232
}
3333

3434
protected override Task<IEnumerable<T>> ReadManyAsync<T>()
3535
{
3636
DecoratedTypeMap.Adapt<T>();
37-
return this._reader.ReadAsync<T>();
37+
return _reader.ReadAsync<T>();
3838
}
3939

40-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TReturn>(Func<TFirst, TSecond, TReturn> map, string splitOn)
40+
protected override IEnumerable<TReturn> ReadMany<TReturn>(Type[] types, Func<object[], TReturn> map, string splitOn)
4141
{
42-
DecoratedTypeMap.Adapt<TFirst, TSecond>();
43-
return this._reader.Read(map, splitOn);
44-
}
45-
46-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TReturn>(Func<TFirst, TSecond, TThird, TReturn> map, string splitOn)
47-
{
48-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird>();
49-
return this._reader.Read(map, splitOn);
50-
}
51-
52-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TReturn> map, string splitOn)
53-
{
54-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth>();
55-
return this._reader.Read(map, splitOn);
56-
}
57-
58-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, string splitOn)
59-
{
60-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth>();
61-
return this._reader.Read(map, splitOn);
62-
}
63-
64-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TReturn> map, string splitOn)
65-
{
66-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>();
67-
return this._reader.Read(map, splitOn);
68-
}
69-
70-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn> map, string splitOn)
71-
{
72-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>();
73-
return this._reader.Read(map, splitOn);
74-
}
75-
76-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TReturn> map, string splitOn)
77-
{
78-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth>();
79-
Type[] types = { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth) };
80-
Func<object[], TReturn> mapWrapper = x => map((TFirst)x[0], (TSecond)x[1], (TThird)x[2], (TFourth)x[3], (TFifth)x[4], (TSixth)x[5], (TSeventh)x[6], (TEighth)x[7], (TNinth)x[8]);
81-
return this._reader.Read(types, mapWrapper, splitOn);
82-
}
83-
84-
protected override IEnumerable<TReturn> ReadMany<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TReturn>(Func<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TReturn> map, string splitOn)
85-
{
86-
DecoratedTypeMap.Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth>();
87-
Type[] types = { typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth), typeof(TTenth) };
88-
Func<object[], TReturn> mapWrapper = x => map((TFirst)x[0], (TSecond)x[1], (TThird)x[2], (TFourth)x[3], (TFifth)x[4], (TSixth)x[5], (TSeventh)x[6], (TEighth)x[7], (TNinth)x[8], (TTenth)x[9]);
89-
return this._reader.Read(types, mapWrapper, splitOn);
42+
DecoratedTypeMap.Adapt(types);
43+
return _reader.Read(types, map, splitOn);
9044
}
9145

9246
protected override T ReadSingle<T>()
9347
{
9448
DecoratedTypeMap.Adapt<T>();
95-
return this._reader.ReadSingle<T>();
49+
return _reader.ReadSingle<T>();
9650
}
9751

9852
protected override Task<T> ReadSingleAsync<T>()
9953
{
10054
DecoratedTypeMap.Adapt<T>();
101-
return this._reader.ReadSingleAsync<T>();
55+
return _reader.ReadSingleAsync<T>();
10256
}
10357

10458
protected override T ReadSingleOrDefault<T>()
10559
{
10660
DecoratedTypeMap.Adapt<T>();
107-
return this._reader.ReadSingleOrDefault<T>();
61+
return _reader.ReadSingleOrDefault<T>();
62+
}
63+
64+
protected override Task<T> ReadSingleOrDefaultAsync<T>()
65+
{
66+
DecoratedTypeMap.Adapt<T>();
67+
return _reader.ReadSingleOrDefaultAsync<T>();
10868
}
10969
#endregion
11070

11171
#region IDisposable Members
11272
public override void Dispose()
11373
{
114-
this._reader?.Dispose();
74+
_reader?.Dispose();
11575
}
11676
#endregion
11777
}

src/Dibix.Dapper/DecoratedTypeMap.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ private DecoratedTypeMap(SqlMapper.ITypeMap inner, Type type)
2020
#endregion
2121

2222
#region Registration
23-
public static void Adapt<T>() => Adapt(typeof(T));
24-
public static void Adapt<TFirst, TSecond>() => Adapt(typeof(TFirst), typeof(TSecond));
25-
public static void Adapt<TFirst, TSecond, TThird>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird));
26-
public static void Adapt<TFirst, TSecond, TThird, TFourth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth));
27-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth));
28-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth));
29-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh));
30-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth));
31-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth));
32-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth), typeof(TTenth));
33-
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth, TNinth, TTenth, TEleventh>() => Adapt(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth), typeof(TNinth), typeof(TTenth), typeof(TEleventh));
34-
private static void Adapt(params Type[] types)
23+
public static void Adapt<T>() => AdaptCore(typeof(T));
24+
public static void Adapt<TFirst, TSecond>() => AdaptCore(typeof(TFirst), typeof(TSecond));
25+
public static void Adapt<TFirst, TSecond, TThird>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird));
26+
public static void Adapt<TFirst, TSecond, TThird, TFourth>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth));
27+
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth));
28+
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth));
29+
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh));
30+
public static void Adapt<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TEighth>() => AdaptCore(typeof(TFirst), typeof(TSecond), typeof(TThird), typeof(TFourth), typeof(TFifth), typeof(TSixth), typeof(TSeventh), typeof(TEighth));
31+
public static void Adapt(Type[] types) => AdaptCore(types);
32+
private static void AdaptCore(params Type[] types)
3533
{
3634
foreach (Type type in types)
3735
Register(type);

0 commit comments

Comments
 (0)