CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
SafeDataReader.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="SafeDataReader.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>This is a DataReader that 'fixes' any null values before</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Data;
10#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
11using System.Data.SqlClient;
12using System.Threading.Tasks;
13#endif
14
15namespace Csla.Data
16{
21 public class SafeDataReader : IDataReader
22 {
23#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
24 private SqlDataReader _sqlDataReader;
25#endif
26
32 protected IDataReader DataReader { get; private set; }
33
39 public SafeDataReader(IDataReader dataReader)
40 {
41 DataReader = dataReader;
42#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
43 _sqlDataReader = DataReader as SqlDataReader;
44#endif
45 }
46
47#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
54 public Task<T> GetFieldValueAsync<T>(int ordinal)
55 {
56 if (_sqlDataReader == null)
57 throw new NotSupportedException("GetFieldValueAsync");
58 return _sqlDataReader.GetFieldValueAsync<T>(ordinal);
59 }
60
67 public Task<T> GetFieldValueAsync<T>(int ordinal, System.Threading.CancellationToken cancellationToken)
68 {
69 if (_sqlDataReader == null)
70 throw new NotSupportedException("GetFieldValueAsync");
71 return _sqlDataReader.GetFieldValueAsync<T>(ordinal, cancellationToken);
72 }
73
80 public Task<bool> IsDbNullAsync(int ordinal)
81 {
82 if (_sqlDataReader == null)
83 throw new NotSupportedException("IsDbNullAsync");
84 return _sqlDataReader.IsDBNullAsync(ordinal);
85 }
86
94 public Task<bool> IsDbNullAsync(int ordinal, System.Threading.CancellationToken cancellationToken)
95 {
96 if (_sqlDataReader == null)
97 throw new NotSupportedException("IsDbNullAsync");
98 return _sqlDataReader.IsDBNullAsync(ordinal, cancellationToken);
99 }
100
105 public Task<bool> NextResultAsync()
106 {
107 if (_sqlDataReader == null)
108 throw new NotSupportedException("NextResultAsync");
109 return _sqlDataReader.NextResultAsync();
110 }
111
117 public Task<bool> NextResultAsync(System.Threading.CancellationToken cancellationToken)
118 {
119 if (_sqlDataReader == null)
120 throw new NotSupportedException("NextResultAsync");
121 return _sqlDataReader.NextResultAsync(cancellationToken);
122 }
123
128 public Task<bool> ReadAsync()
129 {
130 if (_sqlDataReader == null)
131 throw new NotSupportedException("NextResultAsync");
132 return _sqlDataReader.ReadAsync();
133 }
134
140 public Task<bool> ReadAsync(System.Threading.CancellationToken cancellationToken)
141 {
142 if (_sqlDataReader == null)
143 throw new NotSupportedException("NextResultAsync");
144 return _sqlDataReader.ReadAsync(cancellationToken);
145 }
146#endif
147
155 public string GetString(string name)
156 {
157 return GetString(DataReader.GetOrdinal(name));
158 }
159
167 public virtual string GetString(int i)
168 {
169 if (DataReader.IsDBNull(i))
170 return string.Empty;
171 else
172 return DataReader.GetString(i);
173 }
174
175
180 public object GetValue(string name)
181 {
182 return GetValue(DataReader.GetOrdinal(name));
183 }
184
189 public virtual object GetValue(int i)
190 {
191 if (DataReader.IsDBNull(i))
192 return null;
193 else
194 return DataReader.GetValue(i);
195 }
196
204 public int GetInt32(string name)
205 {
206 return GetInt32(DataReader.GetOrdinal(name));
207 }
208
216 public virtual int GetInt32(int i)
217 {
218 if (DataReader.IsDBNull(i))
219 return 0;
220 else
221 return DataReader.GetInt32(i);
222 }
223
231 public double GetDouble(string name)
232 {
233 return GetDouble(DataReader.GetOrdinal(name));
234 }
235
243 public virtual double GetDouble(int i)
244 {
245 if (DataReader.IsDBNull(i))
246 return 0;
247 else
248 return DataReader.GetDouble(i);
249 }
250
259 public Csla.SmartDate GetSmartDate(string name)
260 {
261 return GetSmartDate(DataReader.GetOrdinal(name), true);
262 }
263
272 public virtual Csla.SmartDate GetSmartDate(int i)
273 {
274 return GetSmartDate(i, true);
275 }
276
289 public Csla.SmartDate GetSmartDate(string name, bool minIsEmpty)
290 {
291 return GetSmartDate(DataReader.GetOrdinal(name), minIsEmpty);
292 }
293
302 int i, bool minIsEmpty)
303 {
304 if (DataReader.IsDBNull(i))
305 return new Csla.SmartDate(minIsEmpty);
306 else
307 return new Csla.SmartDate(
308 DataReader.GetDateTime(i), minIsEmpty);
309 }
310
318 public System.Guid GetGuid(string name)
319 {
320 return GetGuid(DataReader.GetOrdinal(name));
321 }
322
330 public virtual System.Guid GetGuid(int i)
331 {
332 if (DataReader.IsDBNull(i))
333 return Guid.Empty;
334 else
335 return DataReader.GetGuid(i);
336 }
337
341 public bool Read()
342 {
343 return DataReader.Read();
344 }
345
349 public bool NextResult()
350 {
351 return DataReader.NextResult();
352 }
353
357 public void Close()
358 {
359 DataReader.Close();
360 }
361
365 public int Depth
366 {
367 get
368 {
369 return DataReader.Depth;
370 }
371 }
372
376 public int FieldCount
377 {
378 get
379 {
380 return DataReader.FieldCount;
381 }
382 }
383
391 public bool GetBoolean(string name)
392 {
393 return GetBoolean(DataReader.GetOrdinal(name));
394 }
395
403 public virtual bool GetBoolean(int i)
404 {
405 if (DataReader.IsDBNull(i))
406 return false;
407 else
408 return DataReader.GetBoolean(i);
409 }
410
418 public byte GetByte(string name)
419 {
420 return GetByte(DataReader.GetOrdinal(name));
421 }
422
430 public virtual byte GetByte(int i)
431 {
432 if (DataReader.IsDBNull(i))
433 return 0;
434 else
435 return DataReader.GetByte(i);
436 }
437
449 public Int64 GetBytes(string name, Int64 fieldOffset,
450 byte[] buffer, int bufferOffset, int length)
451 {
452 return GetBytes(DataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
453 }
454
466 public virtual Int64 GetBytes(int i, Int64 fieldOffset,
467 byte[] buffer, int bufferOffset, int length)
468 {
469 if (DataReader.IsDBNull(i))
470 return 0;
471 else
472 return DataReader.GetBytes(i, fieldOffset, buffer, bufferOffset, length);
473 }
474
482 public char GetChar(string name)
483 {
484 return GetChar(DataReader.GetOrdinal(name));
485 }
486
494 public virtual char GetChar(int i)
495 {
496 if (DataReader.IsDBNull(i))
497 return char.MinValue;
498 else
499 {
500 char[] myChar = new char[1];
501 DataReader.GetChars(i, 0, myChar, 0, 1);
502 return myChar[0];
503 }
504 }
505
517 public Int64 GetChars(string name, Int64 fieldOffset,
518 char[] buffer, int bufferOffset, int length)
519 {
520 return GetChars(DataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
521 }
522
534 public virtual Int64 GetChars(int i, Int64 fieldOffset,
535 char[] buffer, int bufferOffset, int length)
536 {
537 if (DataReader.IsDBNull(i))
538 return 0;
539 else
540 return DataReader.GetChars(i, fieldOffset, buffer, bufferOffset, length);
541 }
542
547 public IDataReader GetData(string name)
548 {
549 return GetData(DataReader.GetOrdinal(name));
550 }
551
556 public virtual IDataReader GetData(int i)
557 {
558 return DataReader.GetData(i);
559 }
560
565 public string GetDataTypeName(string name)
566 {
567 return GetDataTypeName(DataReader.GetOrdinal(name));
568 }
569
574 public virtual string GetDataTypeName(int i)
575 {
576 return DataReader.GetDataTypeName(i);
577 }
578
586 public virtual DateTime GetDateTime(string name)
587 {
588 return GetDateTime(DataReader.GetOrdinal(name));
589 }
590
598 public virtual DateTime GetDateTime(int i)
599 {
600 if (DataReader.IsDBNull(i))
601 return DateTime.MinValue;
602 else
603 return DataReader.GetDateTime(i);
604 }
605
613 public virtual DateTimeOffset GetDateTimeOffset(string name)
614 {
615 return GetDateTimeOffset(DataReader.GetOrdinal(name));
616 }
617
625 public virtual DateTimeOffset GetDateTimeOffset(int i)
626 {
627 if (DataReader.IsDBNull(i))
628 return DateTimeOffset.MinValue;
629 else
630 return (DateTimeOffset)DataReader.GetValue(i);
631 }
632
640 public decimal GetDecimal(string name)
641 {
642 return GetDecimal(DataReader.GetOrdinal(name));
643 }
644
652 public virtual decimal GetDecimal(int i)
653 {
654 if (DataReader.IsDBNull(i))
655 return 0;
656 else
657 return DataReader.GetDecimal(i);
658 }
659
664 public Type GetFieldType(string name)
665 {
666 return GetFieldType(DataReader.GetOrdinal(name));
667 }
668
673 public virtual Type GetFieldType(int i)
674 {
675 return DataReader.GetFieldType(i);
676 }
677
685 public float GetFloat(string name)
686 {
687 return GetFloat(DataReader.GetOrdinal(name));
688 }
689
697 public virtual float GetFloat(int i)
698 {
699 if (DataReader.IsDBNull(i))
700 return 0;
701 else
702 return DataReader.GetFloat(i);
703 }
704
712 public short GetInt16(string name)
713 {
714 return GetInt16(DataReader.GetOrdinal(name));
715 }
716
724 public virtual short GetInt16(int i)
725 {
726 if (DataReader.IsDBNull(i))
727 return 0;
728 else
729 return DataReader.GetInt16(i);
730 }
731
739 public Int64 GetInt64(string name)
740 {
741 return GetInt64(DataReader.GetOrdinal(name));
742 }
743
751 public virtual Int64 GetInt64(int i)
752 {
753 if (DataReader.IsDBNull(i))
754 return 0;
755 else
756 return DataReader.GetInt64(i);
757 }
758
763 public virtual string GetName(int i)
764 {
765 return DataReader.GetName(i);
766 }
767
772 public int GetOrdinal(string name)
773 {
774 return DataReader.GetOrdinal(name);
775 }
776
780 public DataTable GetSchemaTable()
781 {
782 return DataReader.GetSchemaTable();
783 }
784
785
791 public int GetValues(object[] values)
792 {
793 return DataReader.GetValues(values);
794 }
795
799 public bool IsClosed
800 {
801 get
802 {
803 return DataReader.IsClosed;
804 }
805 }
806
811 public virtual bool IsDBNull(int i)
812 {
813 return DataReader.IsDBNull(i);
814 }
815
820 public virtual bool IsDBNull(string name)
821 {
822 int index = this.GetOrdinal(name);
823 return this.IsDBNull(index);
824 }
825
830 public object this[string name]
831 {
832 get
833 {
834 object val = DataReader[name];
835 if (DBNull.Value.Equals(val))
836 return null;
837 else
838 return val;
839 }
840 }
841
846 public virtual object this[int i]
847 {
848 get
849 {
850 if (DataReader.IsDBNull(i))
851 return null;
852 else
853 return DataReader[i];
854 }
855 }
860 {
861 get
862 {
863 return DataReader.RecordsAffected;
864 }
865 }
866
867#region IDisposable Support
868
869 private bool _disposedValue; // To detect redundant calls
870
876 protected virtual void Dispose(bool disposing)
877 {
878 if (!_disposedValue)
879 {
880 if (disposing)
881 {
882 // free unmanaged resources when explicitly called
883 DataReader.Dispose();
884 }
885
886 // free shared unmanaged resources
887 }
888 _disposedValue = true;
889 }
890
894 public void Dispose()
895 {
896 // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
897 Dispose(true);
898 GC.SuppressFinalize(this);
899 }
900
905 {
906 Dispose(false);
907 }
908
909#endregion
910
911 }
912}
This is an IDataReader that 'fixes' any null values before they are returned to our business code.
int GetValues(object[] values)
Invokes the GetValues method of the underlying datareader.
void Dispose()
Disposes the object.
virtual float GetFloat(int i)
Gets a Single value from the datareader.
IDataReader GetData(string name)
Invokes the GetData method of the underlying datareader.
IDataReader DataReader
Get a reference to the underlying data reader object that actually contains the data from the data so...
int RecordsAffected
Returns the RecordsAffected property value from the underlying datareader.
virtual System.Guid GetGuid(int i)
Gets a Guid value from the datareader.
virtual Csla.SmartDate GetSmartDate(int i)
Gets a SmartDate from the datareader.
Task< bool > IsDbNullAsync(int ordinal)
Gets a value indicating whether the column has a null or missing value.
virtual bool GetBoolean(int i)
Gets a boolean value from the datareader.
virtual Int64 GetBytes(int i, Int64 fieldOffset, byte[] buffer, int bufferOffset, int length)
Invokes the GetBytes method of the underlying datareader.
int GetInt32(string name)
Gets an integer from the datareader.
virtual object GetValue(int i)
Gets a value of type System.Object from the datareader.
string GetString(string name)
Gets a string value from the datareader.
Int64 GetChars(string name, Int64 fieldOffset, char[] buffer, int bufferOffset, int length)
Invokes the GetChars method of the underlying datareader.
bool IsClosed
Returns the IsClosed property value from the datareader.
virtual DateTime GetDateTime(string name)
Gets a date value from the datareader.
virtual Csla.SmartDate GetSmartDate(int i, bool minIsEmpty)
Gets a SmartDate from the datareader.
virtual string GetString(int i)
Gets a string value from the datareader.
virtual DateTimeOffset GetDateTimeOffset(int i)
Gets an UTC date value from the datareader.
void Close()
Closes the datareader.
Csla.SmartDate GetSmartDate(string name)
Gets a SmartDate from the datareader.
Task< bool > NextResultAsync()
Advances the reader to the next result.
float GetFloat(string name)
Gets a Single value from the datareader.
virtual Int64 GetChars(int i, Int64 fieldOffset, char[] buffer, int bufferOffset, int length)
Invokes the GetChars method of the underlying datareader.
Int64 GetBytes(string name, Int64 fieldOffset, byte[] buffer, int bufferOffset, int length)
Invokes the GetBytes method of the underlying datareader.
virtual string GetDataTypeName(int i)
Invokes the GetDataTypeName method of the underlying datareader.
string GetDataTypeName(string name)
Invokes the GetDataTypeName method of the underlying datareader.
virtual void Dispose(bool disposing)
Disposes the object.
virtual IDataReader GetData(int i)
Invokes the GetData method of the underlying datareader.
virtual short GetInt16(int i)
Gets a Short value from the datareader.
int Depth
Returns the depth property value from the datareader.
virtual char GetChar(int i)
Gets a char value from the datareader.
bool GetBoolean(string name)
Gets a boolean value from the datareader.
Int64 GetInt64(string name)
Gets a Long value from the datareader.
decimal GetDecimal(string name)
Gets a decimal value from the datareader.
virtual double GetDouble(int i)
Gets a double from the datareader.
Task< bool > IsDbNullAsync(int ordinal, System.Threading.CancellationToken cancellationToken)
Gets a value indicating whether the column has a null or missing value.
bool NextResult()
Moves to the next result set in the datareader.
Task< bool > ReadAsync(System.Threading.CancellationToken cancellationToken)
Advances to the next record in a recordset.
virtual int GetInt32(int i)
Gets an integer from the datareader.
Type GetFieldType(string name)
Invokes the GetFieldType method of the underlying datareader.
Task< T > GetFieldValueAsync< T >(int ordinal)
Asynchronously gets the data value as a type.
object GetValue(string name)
Gets a value of type System.Object from the datareader.
Task< bool > NextResultAsync(System.Threading.CancellationToken cancellationToken)
Advances the reader to the next result.
virtual DateTimeOffset GetDateTimeOffset(string name)
Gets an UTC date value from the datareader.
byte GetByte(string name)
Gets a byte value from the datareader.
virtual Int64 GetInt64(int i)
Gets a Long value from the datareader.
virtual decimal GetDecimal(int i)
Gets a decimal value from the datareader.
virtual bool IsDBNull(int i)
Invokes the IsDBNull method of the underlying datareader.
System.Guid GetGuid(string name)
Gets a Guid value from the datareader.
Task< bool > ReadAsync()
Advances to the next record in a recordset.
virtual Type GetFieldType(int i)
Invokes the GetFieldType method of the underlying datareader.
virtual bool IsDBNull(string name)
Invokes the IsDBNull method of the underlying datareader.
DataTable GetSchemaTable()
Invokes the GetSchemaTable method of the underlying datareader.
virtual byte GetByte(int i)
Gets a byte value from the datareader.
Csla.SmartDate GetSmartDate(string name, bool minIsEmpty)
Gets a SmartDate from the datareader.
int GetOrdinal(string name)
Gets an ordinal value from the datareader.
int FieldCount
Returns the FieldCount property from the datareader.
short GetInt16(string name)
Gets a Short value from the datareader.
virtual DateTime GetDateTime(int i)
Gets a date value from the datareader.
virtual string GetName(int i)
Invokes the GetName method of the underlying datareader.
SafeDataReader(IDataReader dataReader)
Initializes the SafeDataReader object to use data from the provided DataReader object.
bool Read()
Reads the next row of data from the datareader.
char GetChar(string name)
Gets a char value from the datareader.
double GetDouble(string name)
Gets a double from the datareader.
Provides a date data type that understands the concept of an empty date value.
Definition: SmartDate.cs:32