Skip to content

Commit a140cd1

Browse files
committed
Implemented abstraction for IDataReader
1 parent e7d07b8 commit a140cd1

14 files changed

+287
-21
lines changed

src/DataAbstractions.Dapper/DataAccessor.Dapper.cs renamed to src/DataAbstractions.Dapper/DataAccessor/DataAccessor.Dapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
1+
using Dapper;
2+
using System;
23
using System.Collections.Generic;
34
using System.Data;
45
using System.Threading.Tasks;
5-
using Dapper;
66

77
namespace DataAbstractions.Dapper
88
{
@@ -145,7 +145,7 @@ public Task<IEnumerable<TReturn>> QueryAsync<TReturn>(string sql, Type[] types,
145145
_connection.QueryAsync(sql, types, map, param, transaction, buffered, splitOn, commandTimeout, commandType);
146146

147147
public async Task<IGridAccessor> QueryMultipleAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
148-
new GridAccessor( await _connection.QueryMultipleAsync(sql, param, transaction, commandTimeout, commandType));
148+
new GridAccessor(await _connection.QueryMultipleAsync(sql, param, transaction, commandTimeout, commandType));
149149

150150
public async Task<IGridAccessor> QueryMultipleAsync(CommandDefinition command) =>
151151
new GridAccessor(await _connection.QueryMultipleAsync(command));

src/DataAbstractions.Dapper/DataAccessor.cs renamed to src/DataAbstractions.Dapper/DataAccessor/DataAccessor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ public partial class DataAccessor : IDataAccessor
99
private readonly IDbConnection _connection;
1010

1111
public DataAccessor(DbConnection connection)
12-
{
12+
{
1313
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
1414
}
1515

1616
public IDbConnection GetUnderlyingConnection() => _connection;
1717

18+
public IDataReaderAccessor GetDataReaderAbstraction(IDataReader reader)
19+
{
20+
return new DataReaderAccessor(reader);
21+
}
22+
1823
public void Dispose() => _connection.Dispose();
24+
25+
1926
}
2027
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Dapper;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Data;
5+
6+
namespace DataAbstractions.Dapper
7+
{
8+
9+
public partial class DataReaderAccessor : IDataReaderAccessor
10+
{
11+
12+
//Dapper Extensions
13+
public IEnumerable<T> Parse<T>()
14+
{
15+
return _dataReader.Parse<T>();
16+
}
17+
18+
public IEnumerable<object> Parse(Type type)
19+
{
20+
return _dataReader.Parse(type);
21+
}
22+
23+
public IEnumerable<dynamic> Parse()
24+
{
25+
return _dataReader.Parse();
26+
}
27+
28+
public Func<IDataReader, object> GetRowParser(Type type, int startIndex = 0, int length = -1, bool returnNullIfFirstMissing = false)
29+
{
30+
return _dataReader.GetRowParser(type, startIndex, length, returnNullIfFirstMissing);
31+
}
32+
33+
public Func<IDataReader, T> GetRowParser<T>(Type concreteType = null, int startIndex = 0, int length = -1, bool returnNullIfFirstMissing = false)
34+
{
35+
return _dataReader.GetRowParser<T>(concreteType, startIndex, length, returnNullIfFirstMissing);
36+
}
37+
38+
39+
}
40+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System;
2+
using System.Data;
3+
4+
namespace DataAbstractions.Dapper
5+
{
6+
7+
public partial class DataReaderAccessor : IDataReaderAccessor
8+
{
9+
private readonly IDataReader _dataReader;
10+
11+
public DataReaderAccessor(IDataReader dataReader)
12+
{
13+
_dataReader = dataReader;
14+
}
15+
16+
public object this[int i] => _dataReader[i];
17+
18+
public object this[string name] => _dataReader[name];
19+
20+
public int Depth => _dataReader.Depth;
21+
22+
public bool IsClosed => _dataReader.IsClosed;
23+
24+
public int RecordsAffected => _dataReader.RecordsAffected;
25+
26+
public int FieldCount => _dataReader.FieldCount;
27+
28+
public void Close()
29+
{
30+
_dataReader.Close();
31+
}
32+
33+
public void Dispose()
34+
{
35+
_dataReader.Dispose();
36+
}
37+
38+
public bool GetBoolean(int i)
39+
{
40+
return _dataReader.GetBoolean(i);
41+
}
42+
43+
public byte GetByte(int i)
44+
{
45+
return _dataReader.GetByte(i);
46+
}
47+
48+
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
49+
{
50+
return _dataReader.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
51+
}
52+
53+
public char GetChar(int i)
54+
{
55+
return _dataReader.GetChar(i);
56+
}
57+
58+
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
59+
{
60+
return _dataReader.GetChars(i, fieldoffset, buffer, bufferoffset, length);
61+
}
62+
63+
public IDataReader GetData(int i)
64+
{
65+
return _dataReader.GetData(i);
66+
}
67+
68+
public string GetDataTypeName(int i)
69+
{
70+
return _dataReader.GetDataTypeName(i);
71+
}
72+
73+
public DateTime GetDateTime(int i)
74+
{
75+
return _dataReader.GetDateTime(i);
76+
}
77+
78+
public decimal GetDecimal(int i)
79+
{
80+
return _dataReader.GetDecimal(i);
81+
}
82+
83+
public double GetDouble(int i)
84+
{
85+
return _dataReader.GetDouble(i);
86+
}
87+
88+
public Type GetFieldType(int i)
89+
{
90+
return _dataReader.GetFieldType(i);
91+
}
92+
93+
public float GetFloat(int i)
94+
{
95+
return _dataReader.GetFloat(i);
96+
}
97+
98+
public Guid GetGuid(int i)
99+
{
100+
return _dataReader.GetGuid(i);
101+
}
102+
103+
public short GetInt16(int i)
104+
{
105+
return _dataReader.GetInt16(i);
106+
}
107+
108+
public int GetInt32(int i)
109+
{
110+
return _dataReader.GetInt32(i);
111+
112+
}
113+
114+
public long GetInt64(int i)
115+
{
116+
return _dataReader.GetInt64(i);
117+
}
118+
119+
public string GetName(int i)
120+
{
121+
return _dataReader.GetName(i);
122+
123+
}
124+
125+
public int GetOrdinal(string name)
126+
{
127+
return _dataReader.GetOrdinal(name);
128+
}
129+
130+
131+
132+
public DataTable GetSchemaTable()
133+
{
134+
return _dataReader.GetSchemaTable();
135+
}
136+
137+
public string GetString(int i)
138+
{
139+
return _dataReader.GetString(i);
140+
141+
}
142+
143+
public object GetValue(int i)
144+
{
145+
return _dataReader.GetValue(i);
146+
147+
}
148+
149+
public int GetValues(object[] values)
150+
{
151+
return _dataReader.GetValues(values);
152+
153+
}
154+
155+
public bool IsDBNull(int i)
156+
{
157+
return _dataReader.IsDBNull(i);
158+
159+
}
160+
161+
public bool NextResult()
162+
{
163+
return _dataReader.NextResult();
164+
165+
}
166+
167+
public bool Read()
168+
{
169+
return _dataReader.Read();
170+
171+
}
172+
173+
}
174+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using System.Threading.Tasks;
4+
5+
namespace DataAbstractions.Dapper
6+
{
7+
public partial interface IDataAccessor : IDbConnection
8+
{
9+
10+
//Dapper.Contrib methods
11+
Task<T> GetAsync<T>(dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
12+
Task<IEnumerable<T>> GetAllAsync<T>(IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
13+
Task<int> InsertAsync<T>(T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class;
14+
Task<bool> UpdateAsync<T>(T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
15+
Task<bool> DeleteAsync<T>(T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
16+
Task<bool> DeleteAllAsync<T>(IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
17+
T Get<T>(dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
18+
IEnumerable<T> GetAll<T>(IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
19+
long Insert<T>(T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
20+
bool Update<T>(T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
21+
bool Delete<T>(T entityToDelete, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
22+
bool DeleteAll<T>(IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
23+
24+
}
25+
26+
27+
}
28+

0 commit comments

Comments
 (0)