Commit for development environment setup

This commit is contained in:
2023-06-19 16:12:33 -04:00
parent be72063a3c
commit bbce2ad0a6
2209 changed files with 1171775 additions and 625 deletions

View File

@@ -0,0 +1,230 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using Csla.Properties;
namespace Csla.Data
{
/// <summary>
/// Map data from a source into a target object
/// by copying public property values.
/// </summary>
/// <remarks></remarks>
public static class DataMapper
{
#region Map from IDictionary
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">A name/value dictionary containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <remarks>
/// The key names in the dictionary must match the property names on the target
/// object. Target properties may not be readonly or indexed.
/// </remarks>
public static void Map(System.Collections.IDictionary source, object target)
{
Map(source, target, false);
}
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">A name/value dictionary containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <param name="ignoreList">A list of property names to ignore.
/// These properties will not be set on the target object.</param>
/// <remarks>
/// The key names in the dictionary must match the property names on the target
/// object. Target properties may not be readonly or indexed.
/// </remarks>
public static void Map(System.Collections.IDictionary source, object target, params string[] ignoreList)
{
Map(source, target, false, ignoreList);
}
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">A name/value dictionary containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <param name="ignoreList">A list of property names to ignore.
/// These properties will not be set on the target object.</param>
/// <param name="suppressExceptions">If <see langword="true" />, any exceptions will be supressed.</param>
/// <remarks>
/// The key names in the dictionary must match the property names on the target
/// object. Target properties may not be readonly or indexed.
/// </remarks>
public static void Map(
System.Collections.IDictionary source,
object target, bool suppressExceptions,
params string[] ignoreList)
{
List<string> ignore = new List<string>(ignoreList);
foreach (string propertyName in source.Keys)
{
if (!ignore.Contains(propertyName))
{
try
{
SetPropertyValue(target, propertyName, source[propertyName]);
}
catch (Exception ex)
{
if (!suppressExceptions)
throw new ArgumentException(
String.Format("{0} ({1})",
Resources.PropertyCopyFailed, propertyName), ex);
}
}
}
}
#endregion
#region Map from Object
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">An object containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <remarks>
/// The property names and types of the source object must match the property names and types
/// on the target object. Source properties may not be indexed.
/// Target properties may not be readonly or indexed.
/// </remarks>
public static void Map(object source, object target)
{
Map(source, target, false);
}
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">An object containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <param name="ignoreList">A list of property names to ignore.
/// These properties will not be set on the target object.</param>
/// <remarks>
/// The property names and types of the source object must match the property names and types
/// on the target object. Source properties may not be indexed.
/// Target properties may not be readonly or indexed.
/// </remarks>
public static void Map(object source, object target, params string[] ignoreList)
{
Map(source, target, false, ignoreList);
}
/// <summary>
/// Copies values from the source into the
/// properties of the target.
/// </summary>
/// <param name="source">An object containing the source values.</param>
/// <param name="target">An object with properties to be set from the dictionary.</param>
/// <param name="ignoreList">A list of property names to ignore.
/// These properties will not be set on the target object.</param>
/// <param name="suppressExceptions">If <see langword="true" />, any exceptions will be supressed.</param>
/// <remarks>
/// <para>
/// The property names and types of the source object must match the property names and types
/// on the target object. Source properties may not be indexed.
/// Target properties may not be readonly or indexed.
/// </para><para>
/// Properties to copy are determined based on the source object. Any properties
/// on the source object marked with the <see cref="BrowsableAttribute"/> equal
/// to false are ignored.
/// </para>
/// </remarks>
public static void Map(
object source, object target,
bool suppressExceptions,
params string[] ignoreList)
{
List<string> ignore = new List<string>(ignoreList);
PropertyInfo[] sourceProperties =
GetSourceProperties(source.GetType());
foreach (PropertyInfo sourceProperty in sourceProperties)
{
string propertyName = sourceProperty.Name;
if (!ignore.Contains(propertyName))
{
try
{
SetPropertyValue(
target, propertyName,
sourceProperty.GetValue(source, null));
}
catch (Exception ex)
{
if (!suppressExceptions)
throw new ArgumentException(
String.Format("{0} ({1})",
Resources.PropertyCopyFailed, propertyName), ex);
}
}
}
}
private static PropertyInfo[] GetSourceProperties(Type sourceType)
{
List<PropertyInfo> result = new List<PropertyInfo>();
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(sourceType);
foreach (PropertyDescriptor item in props)
if (item.IsBrowsable)
result.Add(sourceType.GetProperty(item.Name));
return result.ToArray();
}
#endregion
/// <summary>
/// Sets an object's property with the specified value,
/// coercing that value to the appropriate type if possible.
/// </summary>
/// <param name="target">Object containing the property to set.</param>
/// <param name="propertyName">Name of the property to set.</param>
/// <param name="value">Value to set into the property.</param>
public static void SetPropertyValue(
object target, string propertyName, object value)
{
PropertyInfo propertyInfo =
target.GetType().GetProperty(propertyName);
if (value == null)
propertyInfo.SetValue(target, value, null);
else
{
Type pType =
Utilities.GetPropertyType(propertyInfo.PropertyType);
Type vType =
Utilities.GetPropertyType(value.GetType());
if (pType.Equals(vType))
{
// types match, just copy value
propertyInfo.SetValue(target, value, null);
}
else
{
// types don't match, try to coerce
if (pType.Equals(typeof(Guid)))
propertyInfo.SetValue(
target, new Guid(value.ToString()), null);
else if (pType.IsEnum && vType.Equals(typeof(string)))
propertyInfo.SetValue(target, Enum.Parse(pType, value.ToString()), null);
else
propertyInfo.SetValue(
target, Convert.ChangeType(value, pType), null);
}
}
}
}
}

View File

@@ -0,0 +1,267 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel;
using System.Reflection;
using Csla.Properties;
namespace Csla.Data
{
/// <summary>
/// An ObjectAdapter is used to convert data in an object
/// or collection into a DataTable.
/// </summary>
public class ObjectAdapter
{
/// <summary>
/// Fills the DataSet with data from an object or collection.
/// </summary>
/// <remarks>
/// The name of the DataTable being filled is will be the class name of
/// the object acting as the data source. The
/// DataTable will be inserted if it doesn't already exist in the DataSet.
/// </remarks>
/// <param name="ds">A reference to the DataSet to be filled.</param>
/// <param name="source">A reference to the object or collection acting as a data source.</param>
public void Fill(DataSet ds, object source)
{
string className = source.GetType().Name;
Fill(ds, className, source);
}
/// <summary>
/// Fills the DataSet with data from an object or collection.
/// </summary>
/// <remarks>
/// The name of the DataTable being filled is specified as a parameter. The
/// DataTable will be inserted if it doesn't already exist in the DataSet.
/// </remarks>
/// <param name="ds">A reference to the DataSet to be filled.</param>
/// <param name="tableName"></param>
/// <param name="source">A reference to the object or collection acting as a data source.</param>
public void Fill(DataSet ds, string tableName, object source)
{
DataTable dt;
bool exists;
dt = ds.Tables[tableName];
exists = (dt != null);
if (!exists)
dt = new DataTable(tableName);
Fill(dt, source);
if (!exists)
ds.Tables.Add(dt);
}
/// <summary>
/// Fills a DataTable with data values from an object or collection.
/// </summary>
/// <param name="dt">A reference to the DataTable to be filled.</param>
/// <param name="source">A reference to the object or collection acting as a data source.</param>
public void Fill(DataTable dt, object source)
{
if (source == null)
throw new ArgumentException(Resources.NothingNotValid);
// get the list of columns from the source
List<string> columns = GetColumns(source);
if (columns.Count < 1) return;
// create columns in DataTable if needed
foreach (string column in columns)
if (!dt.Columns.Contains(column))
dt.Columns.Add(column);
// get an IList and copy the data
CopyData(dt, GetIList(source), columns);
}
#region DataCopyIList
private IList GetIList(object source)
{
if (source is IListSource)
return ((IListSource)source).GetList();
else if (source is IList)
return source as IList;
else
{
// this is a regular object - create a list
ArrayList col = new ArrayList();
col.Add(source);
return col;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
private void CopyData(
DataTable dt, IList ds, List<string> columns)
{
// load the data into the DataTable
dt.BeginLoadData();
for (int index = 0; index < ds.Count; index++)
{
DataRow dr = dt.NewRow();
foreach (string column in columns)
{
try
{
dr[column] = GetField(ds[index], column);
}
catch (Exception ex)
{
dr[column] = ex.Message;
}
}
dt.Rows.Add(dr);
}
dt.EndLoadData();
}
#endregion
#region GetColumns
private List<string> GetColumns(object source)
{
List<string> result;
// first handle DataSet/DataTable
object innerSource;
IListSource iListSource = source as IListSource;
if (iListSource != null)
innerSource = iListSource.GetList();
else
innerSource = source;
DataView dataView = innerSource as DataView;
if (dataView != null)
result = ScanDataView(dataView);
else
{
// now handle lists/arrays/collections
IEnumerable iEnumerable = innerSource as IEnumerable;
if (iEnumerable != null)
{
Type childType = Utilities.GetChildItemType(
innerSource.GetType());
result = ScanObject(childType);
}
else
{
// the source is a regular object
result = ScanObject(innerSource.GetType());
}
}
return result;
}
private List<string> ScanDataView(DataView ds)
{
List<string> result = new List<string>();
for (int field = 0; field < ds.Table.Columns.Count; field++)
result.Add(ds.Table.Columns[field].ColumnName);
return result;
}
private List<string> ScanObject(Type sourceType)
{
List<string> result = new List<string>();
if (sourceType != null)
{
// retrieve a list of all public properties
PropertyInfo[] props = sourceType.GetProperties();
if (props.Length >= 0)
for (int column = 0; column < props.Length; column++)
if (props[column].CanRead)
result.Add(props[column].Name);
// retrieve a list of all public fields
FieldInfo[] fields = sourceType.GetFields();
if (fields.Length >= 0)
for (int column = 0; column < fields.Length; column++)
result.Add(fields[column].Name);
}
return result;
}
#endregion
#region GetField
private static string GetField(object obj, string fieldName)
{
string result;
DataRowView dataRowView = obj as DataRowView;
if (dataRowView != null)
{
// this is a DataRowView from a DataView
result = dataRowView[fieldName].ToString();
}
else if (obj is ValueType && obj.GetType().IsPrimitive)
{
// this is a primitive value type
result = obj.ToString();
}
else
{
string tmp = obj as string;
if (tmp != null)
{
// this is a simple string
result = (string)obj;
}
else
{
// this is an object or Structure
try
{
Type sourceType = obj.GetType();
// see if the field is a property
PropertyInfo prop = sourceType.GetProperty(fieldName);
if ((prop == null) || (!prop.CanRead))
{
// no readable property of that name exists -
// check for a field
FieldInfo field = sourceType.GetField(fieldName);
if (field == null)
{
// no field exists either, throw an exception
throw new DataException(
Resources.NoSuchValueExistsException +
" " + fieldName);
}
else
{
// got a field, return its value
result = field.GetValue(obj).ToString();
}
}
else
{
// found a property, return its value
result = prop.GetValue(obj, null).ToString();
}
}
catch (Exception ex)
{
throw new DataException(
Resources.ErrorReadingValueException +
" " + fieldName, ex);
}
}
}
return result;
}
#endregion
}
}

View File

@@ -0,0 +1,761 @@
using System;
using System.Data;
namespace Csla.Data
{
/// <summary>
/// This is a DataReader that 'fixes' any null values before
/// they are returned to our business code.
/// </summary>
public class SafeDataReader : IDataReader
{
private IDataReader _dataReader;
/// <summary>
/// Get a reference to the underlying data reader
/// object that actually contains the data from
/// the data source.
/// </summary>
protected IDataReader DataReader
{
get { return _dataReader; }
}
/// <summary>
/// Initializes the SafeDataReader object to use data from
/// the provided DataReader object.
/// </summary>
/// <param name="dataReader">The source DataReader object containing the data.</param>
public SafeDataReader(IDataReader dataReader)
{
_dataReader = dataReader;
}
/// <summary>
/// Gets a string value from the datareader.
/// </summary>
/// <remarks>
/// Returns empty string for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public string GetString(string name)
{
return GetString(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a string value from the datareader.
/// </summary>
/// <remarks>
/// Returns empty string for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetString(int i)
{
if (_dataReader.IsDBNull(i))
return string.Empty;
else
return _dataReader.GetString(i);
}
/// <summary>
/// Gets a value of type <see cref="System.Object" /> from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public object GetValue(string name)
{
return GetValue(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a value of type <see cref="System.Object" /> from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual object GetValue(int i)
{
if (_dataReader.IsDBNull(i))
return null;
else
return _dataReader.GetValue(i);
}
/// <summary>
/// Gets an integer from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public int GetInt32(string name)
{
return GetInt32(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets an integer from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual int GetInt32(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetInt32(i);
}
/// <summary>
/// Gets a double from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public double GetDouble(string name)
{
return GetDouble(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a double from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual double GetDouble(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetDouble(i);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into min possible date
/// See Chapter 5 for more details on the SmartDate class.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public Csla.SmartDate GetSmartDate(string name)
{
return GetSmartDate(_dataReader.GetOrdinal(name), true);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into the min possible date
/// See Chapter 5 for more details on the SmartDate class.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual Csla.SmartDate GetSmartDate(int i)
{
return GetSmartDate(i, true);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into either the min or max possible date
/// depending on the MinIsEmpty parameter. See Chapter 5 for more
/// details on the SmartDate class.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="minIsEmpty">
/// A flag indicating whether the min or max
/// value of a data means an empty date.</param>
public Csla.SmartDate GetSmartDate(string name, bool minIsEmpty)
{
return GetSmartDate(_dataReader.GetOrdinal(name), minIsEmpty);
}
/// <summary>
/// Gets a <see cref="SmartDate"/> from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="minIsEmpty">
/// A flag indicating whether the min or max
/// value of a data means an empty date.</param>
public virtual Csla.SmartDate GetSmartDate(
int i, bool minIsEmpty)
{
if (_dataReader.IsDBNull(i))
return new Csla.SmartDate(minIsEmpty);
else
return new Csla.SmartDate(
_dataReader.GetDateTime(i), minIsEmpty);
}
/// <summary>
/// Gets a Guid value from the datareader.
/// </summary>
/// <remarks>
/// Returns Guid.Empty for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public System.Guid GetGuid(string name)
{
return GetGuid(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Guid value from the datareader.
/// </summary>
/// <remarks>
/// Returns Guid.Empty for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual System.Guid GetGuid(int i)
{
if (_dataReader.IsDBNull(i))
return Guid.Empty;
else
return _dataReader.GetGuid(i);
}
/// <summary>
/// Reads the next row of data from the datareader.
/// </summary>
public bool Read()
{
return _dataReader.Read();
}
/// <summary>
/// Moves to the next result set in the datareader.
/// </summary>
public bool NextResult()
{
return _dataReader.NextResult();
}
/// <summary>
/// Closes the datareader.
/// </summary>
public void Close()
{
_dataReader.Close();
}
/// <summary>
/// Returns the depth property value from the datareader.
/// </summary>
public int Depth
{
get
{
return _dataReader.Depth;
}
}
/// <summary>
/// Returns the FieldCount property from the datareader.
/// </summary>
public int FieldCount
{
get
{
return _dataReader.FieldCount;
}
}
/// <summary>
/// Gets a boolean value from the datareader.
/// </summary>
/// <remarks>
/// Returns <see langword="false" /> for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public bool GetBoolean(string name)
{
return GetBoolean(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a boolean value from the datareader.
/// </summary>
/// <remarks>
/// Returns <see langword="false" /> for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual bool GetBoolean(int i)
{
if (_dataReader.IsDBNull(i))
return false;
else
return _dataReader.GetBoolean(i);
}
/// <summary>
/// Gets a byte value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public byte GetByte(string name)
{
return GetByte(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a byte value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual byte GetByte(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetByte(i);
}
/// <summary>
/// Invokes the GetBytes method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public Int64 GetBytes(string name, Int64 fieldOffset,
byte[] buffer, int bufferOffset, int length)
{
return GetBytes(_dataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetBytes method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public virtual Int64 GetBytes(int i, Int64 fieldOffset,
byte[] buffer, int bufferOffset, int length)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetBytes(i, fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Gets a char value from the datareader.
/// </summary>
/// <remarks>
/// Returns Char.MinValue for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public char GetChar(string name)
{
return GetChar(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a char value from the datareader.
/// </summary>
/// <remarks>
/// Returns Char.MinValue for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual char GetChar(int i)
{
if (_dataReader.IsDBNull(i))
return char.MinValue;
else
{
char[] myChar = new char[1];
_dataReader.GetChars(i, 0, myChar, 0, 1);
return myChar[0];
}
}
/// <summary>
/// Invokes the GetChars method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public Int64 GetChars(string name, Int64 fieldOffset,
char[] buffer, int bufferOffset, int length)
{
return GetChars(_dataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetChars method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public virtual Int64 GetChars(int i, Int64 fieldOffset,
char[] buffer, int bufferOffset, int length)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetChars(i, fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetData method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public IDataReader GetData(string name)
{
return GetData(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetData method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual IDataReader GetData(int i)
{
return _dataReader.GetData(i);
}
/// <summary>
/// Invokes the GetDataTypeName method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public string GetDataTypeName(string name)
{
return GetDataTypeName(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetDataTypeName method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetDataTypeName(int i)
{
return _dataReader.GetDataTypeName(i);
}
/// <summary>
/// Gets a date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTime.MinValue for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public virtual DateTime GetDateTime(string name)
{
return GetDateTime(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTime.MinValue for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual DateTime GetDateTime(int i)
{
if (_dataReader.IsDBNull(i))
return DateTime.MinValue;
else
return _dataReader.GetDateTime(i);
}
/// <summary>
/// Gets a decimal value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public decimal GetDecimal(string name)
{
return GetDecimal(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a decimal value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual decimal GetDecimal(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetDecimal(i);
}
/// <summary>
/// Invokes the GetFieldType method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public Type GetFieldType(string name)
{
return GetFieldType(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetFieldType method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual Type GetFieldType(int i)
{
return _dataReader.GetFieldType(i);
}
/// <summary>
/// Gets a Single value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public float GetFloat(string name)
{
return GetFloat(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Single value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual float GetFloat(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetFloat(i);
}
/// <summary>
/// Gets a Short value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public short GetInt16(string name)
{
return GetInt16(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Short value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual short GetInt16(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetInt16(i);
}
/// <summary>
/// Gets a Long value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
public Int64 GetInt64(string name)
{
return GetInt64(_dataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Long value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual Int64 GetInt64(int i)
{
if (_dataReader.IsDBNull(i))
return 0;
else
return _dataReader.GetInt64(i);
}
/// <summary>
/// Invokes the GetName method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetName(int i)
{
return _dataReader.GetName(i);
}
/// <summary>
/// Gets an ordinal value from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public int GetOrdinal(string name)
{
return _dataReader.GetOrdinal(name);
}
/// <summary>
/// Invokes the GetSchemaTable method of the underlying datareader.
/// </summary>
public DataTable GetSchemaTable()
{
return _dataReader.GetSchemaTable();
}
/// <summary>
/// Invokes the GetValues method of the underlying datareader.
/// </summary>
/// <param name="values">An array of System.Object to
/// copy the values into.</param>
public int GetValues(object[] values)
{
return _dataReader.GetValues(values);
}
/// <summary>
/// Returns the IsClosed property value from the datareader.
/// </summary>
public bool IsClosed
{
get
{
return _dataReader.IsClosed;
}
}
/// <summary>
/// Invokes the IsDBNull method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual bool IsDBNull(int i)
{
return _dataReader.IsDBNull(i);
}
/// <summary>
/// Returns a value from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
public object this[string name]
{
get
{
object val = _dataReader[name];
if (DBNull.Value.Equals(val))
return null;
else
return val;
}
}
/// <summary>
/// Returns a value from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual object this[int i]
{
get
{
if (_dataReader.IsDBNull(i))
return null;
else
return _dataReader[i];
}
}
/// <summary>
/// Returns the RecordsAffected property value from the underlying datareader.
/// </summary>
public int RecordsAffected
{
get
{
return _dataReader.RecordsAffected;
}
}
#region IDisposable Support
private bool _disposedValue; // To detect redundant calls
/// <summary>
/// Disposes the object.
/// </summary>
/// <param name="disposing">True if called by
/// the public Dispose method.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// free unmanaged resources when explicitly called
_dataReader.Dispose();
}
// free shared unmanaged resources
}
_disposedValue = true;
}
/// <summary>
/// Disposes the object.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Object finalizer.
/// </summary>
~SafeDataReader()
{
Dispose(false);
}
#endregion
}
}