sharp-chat/SharpChatCommon/Data/DbObjectEnumerator.cs

37 lines
815 B
C#

using System.Collections;
using System.Data.Common;
namespace SharpChat.Data;
public class DbObjectEnumerator<T>(DbDataReader reader, Func<DbDataReader, T> constructor, Action? dispose) : IEnumerator<T> {
public T Current => constructor(reader);
object? IEnumerator.Current => Current;
public bool MoveNext() {
return reader.Read();
}
public void Reset() {
// no-op cus you can't reset a DbDataReader afaik
}
private bool disposed;
~DbObjectEnumerator() {
DoDispose();
}
public void Dispose() {
DoDispose();
GC.SuppressFinalize(this);
}
private void DoDispose() {
if(disposed)
return;
disposed = true;
reader.Dispose();
if(dispose is not null)
dispose();
}
}