은 정말 제이슨의 솔루션에 비슷한 제안, 대신 IDataReader에를 구현하는 래퍼를 사용하는 것이 모든 영향을받지 속성에 대한 통과 코드의 꽤 필요로한다는 것을
가
sealed public class PeekDataReader : IDataReader
{
private IDataReader wrappedReader;
private bool wasPeeked;
private bool lastResult;
public PeekDataReader(IDataReader wrappedReader)
{
this.wrappedReader = wrappedReader;
}
public bool Peek()
{
// If the previous operation was a peek, do not move...
if (this.wasPeeked)
return this.lastResult;
// This is the first peek for the current position, so read and tag
bool result = Read();
this.wasPeeked = true;
return result;
}
public bool Read()
{
// If last operation was a peek, do not actually read
if (this.wasPeeked)
{
this.wasPeeked = false;
return this.lastResult;
}
// Remember the result for any subsequent peeks
this.lastResult = this.wrappedReader.Read();
return this.lastResult;
}
public bool NextResult()
{
this.wasPeeked = false;
return this.wrappedReader.NextResult();
}
// Add pass-through operations for all other IDataReader methods
// that simply call on 'this.wrappedReader'
}
주, 그러나 이점은 후속 '읽기'작업을 진행하지 않고 결과 집합의 어느 위치에서나 '엿보기'할 수있는 일반적인 추상화라는 점입니다.
사용하려면
using (IDataReader reader = new PeekDataReader(/* actual reader */))
{
if (reader.Peek())
{
// perform some operations on the first row if it exists...
}
while (reader.Read())
{
// re-use the first row, and then read the remainder...
}
}
참고 그 모든 '픽() 호출은 실제로 다음 레코드로 이동합니다하지만 이전 작업은 또한'픽() '가 아니었다면. 'Read()'연산을 통해이 대칭을 유지하면 구현이 간단하고 고급 API가 제공됩니다.
'IDataReader' 인터페이스 자체에'.Peek' 메소드가 없으므로 예제가 작동하지 않습니다. 명시 적으로 scope 변수를'PeekDataReader'로 입력하거나'var'을 사용해야합니다. – julealgon