Skip to content

Commit 2c59b37

Browse files
maca88hazzik
authored andcommitted
Refactored custom data readers to be created and initialized within a factory method in order to ensure that the instance will always be fully initialized.
1 parent d8f97ce commit 2c59b37

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)
239239

240240
if (!_factory.ConnectionProvider.Driver.SupportsMultipleOpenReaders)
241241
{
242-
reader = new NHybridDataReader().Initialize(reader);
242+
reader = NHybridDataReader.Create(reader);
243243
}
244244

245245
_readersToClose.Add(reader);

src/NHibernate/Driver/BasicResultSetsCommand.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public virtual DbDataReader GetReader(int? commandTimeout)
5353
}
5454
log.Info(command.CommandText);
5555
BindParameters(command);
56-
return new BatcherDataReaderWrapper(batcher, command).Initialize();
56+
return BatcherDataReaderWrapper.Create(batcher, command);
5757
}
5858

5959
protected virtual void BindParameters(DbCommand command)
@@ -86,7 +86,7 @@ public class BatcherDataReaderWrapper: DbDataReader
8686
private readonly DbCommand command;
8787
private DbDataReader reader;
8888

89-
public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
89+
protected BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
9090
{
9191
if (batcher == null)
9292
{
@@ -100,10 +100,12 @@ public BatcherDataReaderWrapper(IBatcher batcher, DbCommand command)
100100
this.command = command;
101101
}
102102

103-
public BatcherDataReaderWrapper Initialize()
103+
public static BatcherDataReaderWrapper Create(IBatcher batcher, DbCommand command)
104104
{
105-
reader = batcher.ExecuteReader(command);
106-
return this;
105+
return new BatcherDataReaderWrapper(batcher, command)
106+
{
107+
reader = batcher.ExecuteReader(command)
108+
};
107109
}
108110

109111
public override string GetName(int i)

src/NHibernate/Driver/NDataReader.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class NDataReader : DbDataReader
3232
private char[] cachedCharArray;
3333
private int cachedColIndex = -1;
3434

35+
protected NDataReader() { }
36+
3537
/// <summary>
3638
/// Creates a NDataReader from a <see cref="DbDataReader" />
3739
/// </summary>
@@ -43,8 +45,9 @@ public class NDataReader : DbDataReader
4345
/// pick up the <see cref="DbDataReader"/> midstream so that the underlying <see cref="DbDataReader"/> can be closed
4446
/// so a new one can be opened.
4547
/// </remarks>
46-
public NDataReader Initialize(DbDataReader reader, bool isMidstream)
48+
public static NDataReader Create(DbDataReader reader, bool isMidstream)
4749
{
50+
var dataReader = new NDataReader();
4851
var resultList = new List<NResult>(2);
4952

5053
try
@@ -53,19 +56,19 @@ public NDataReader Initialize(DbDataReader reader, bool isMidstream)
5356
// positioned on the first row (index=0)
5457
if (isMidstream)
5558
{
56-
currentRowIndex = 0;
59+
dataReader.currentRowIndex = 0;
5760
}
5861

5962
// there will be atleast one result
60-
resultList.Add(new NResult().Initialize(reader, isMidstream));
63+
resultList.Add(NResult.Create(reader, isMidstream));
6164

6265
while (reader.NextResult())
6366
{
6467
// the second, third, nth result is not processed midstream
65-
resultList.Add(new NResult().Initialize(reader, false));
68+
resultList.Add(NResult.Create(reader, false));
6669
}
6770

68-
results = resultList.ToArray();
71+
dataReader.results = resultList.ToArray();
6972
}
7073
catch (Exception e)
7174
{
@@ -75,7 +78,7 @@ public NDataReader Initialize(DbDataReader reader, bool isMidstream)
7578
{
7679
reader.Close();
7780
}
78-
return this;
81+
return dataReader;
7982
}
8083

8184
/// <summary>
@@ -476,6 +479,8 @@ private class NResult
476479
private readonly IList<System.Type> fieldTypes = new List<System.Type>();
477480
private readonly IList<string> fieldDataTypeNames = new List<string>();
478481

482+
private NResult() { }
483+
479484
/// <summary>
480485
/// Initializes a new instance of the NResult class.
481486
/// </summary>
@@ -484,9 +489,12 @@ private class NResult
484489
/// <see langword="true" /> if the <see cref="DbDataReader"/> is already positioned on the record
485490
/// to start reading from.
486491
/// </param>
487-
internal NResult Initialize(DbDataReader reader, bool isMidstream)
492+
internal static NResult Create(DbDataReader reader, bool isMidstream)
488493
{
489-
schemaTable = reader.GetSchemaTable();
494+
var result = new NResult
495+
{
496+
schemaTable = reader.GetSchemaTable()
497+
};
490498

491499
List<object[]> recordsList = new List<object[]>();
492500
int rowIndex = 0;
@@ -500,13 +508,13 @@ internal NResult Initialize(DbDataReader reader, bool isMidstream)
500508
for (int i = 0; i < reader.FieldCount; i++)
501509
{
502510
string fieldName = reader.GetName(i);
503-
fieldNameToIndex[fieldName] = i;
504-
fieldIndexToName.Add(fieldName);
505-
fieldTypes.Add(reader.GetFieldType(i));
506-
fieldDataTypeNames.Add(reader.GetDataTypeName(i));
511+
result.fieldNameToIndex[fieldName] = i;
512+
result.fieldIndexToName.Add(fieldName);
513+
result.fieldTypes.Add(reader.GetFieldType(i));
514+
result.fieldDataTypeNames.Add(reader.GetDataTypeName(i));
507515
}
508516

509-
colCount = reader.FieldCount;
517+
result.colCount = reader.FieldCount;
510518
}
511519

512520
rowIndex++;
@@ -520,8 +528,8 @@ internal NResult Initialize(DbDataReader reader, bool isMidstream)
520528
isMidstream = false;
521529
}
522530

523-
records = recordsList.ToArray();
524-
return this;
531+
result.records = recordsList.ToArray();
532+
return result;
525533
}
526534

527535
/// <summary>

src/NHibernate/Driver/NHybridDataReader.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,34 @@ public class NHybridDataReader : DbDataReader
3030

3131
public DbDataReader Target { get { return _reader; } }
3232

33+
protected NHybridDataReader() { }
34+
3335
/// <summary>
3436
/// Initializes a new instance of the <see cref="NHybridDataReader"/> class.
3537
/// </summary>
3638
/// <param name="reader">The underlying DbDataReader to use.</param>
37-
public NHybridDataReader Initialize(DbDataReader reader)
39+
public static NHybridDataReader Create(DbDataReader reader)
3840
{
39-
return Initialize(reader, false);
41+
return Create(reader, false);
4042
}
4143

4244
/// <summary>
4345
/// Initializes a new instance of the NHybridDataReader class.
4446
/// </summary>
4547
/// <param name="reader">The underlying DbDataReader to use.</param>
4648
/// <param name="inMemory"><see langword="true" /> if the contents of the DbDataReader should be read into memory right away.</param>
47-
public NHybridDataReader Initialize(DbDataReader reader, bool inMemory)
49+
public static NHybridDataReader Create(DbDataReader reader, bool inMemory)
4850
{
51+
var dataReader = new NHybridDataReader();
4952
if (inMemory)
5053
{
51-
_reader = new NDataReader().Initialize(reader, false);
54+
dataReader._reader = NDataReader.Create(reader, false);
5255
}
5356
else
5457
{
55-
_reader = reader;
58+
dataReader._reader = reader;
5659
}
57-
return this;
60+
return dataReader;
5861
}
5962

6063
/// <summary>
@@ -72,7 +75,7 @@ public void ReadIntoMemory()
7275
{
7376
log.Debug("Moving DbDataReader into an NDataReader. It was converted in midstream " + _isMidstream.ToString());
7477
}
75-
_reader = new NDataReader().Initialize(_reader, _isMidstream);
78+
_reader = NDataReader.Create(_reader, _isMidstream);
7679
}
7780
}
7881

0 commit comments

Comments
 (0)