/// <summary> /// Factory method to create the appropriate adapter based on a data source. /// </summary> /// <param name="dataSource">The data source to create an adapter for.</param> /// <returns>Specific data source adapter instance.</returns> /// <exception cref="ArgumentNullException">Thrown if the dataSource paramter is null.</exception> /// <exception cref="NotSupportedException">Thrown if the dataSource is not of a supported type.</exception>" internal static DataSourceAdapter <TItem> CreateAdapter(object dataSource) { if (dataSource == null) { throw new ArgumentException("dataSource cannot be null", "dataSource"); } DataSourceAdapter <TItem> adapter; Type dataSourceType = dataSource.GetType(); if (dataSourceType == typeof(DataTable)) { adapter = new DataTableDataSourceAdapter <TItem>((DataTable)dataSource); } else if (typeof(IDataReader).IsAssignableFrom(dataSourceType)) { adapter = new DataReaderDataSourceAdapter <TItem>((IDataReader)dataSource); } else if (typeof(IEnumerable).IsAssignableFrom(dataSourceType)) { adapter = new EnumerableDataSourceAdapter <TItem>(dataSource); } else if (dataSourceType == typeof(ObjectDataSource)) { adapter = new ObjectDataSourceDataSourceAdapter <TItem>((ObjectDataSource)dataSource); } else { throw new NotSupportedException(String.Format("{0} type not supported", dataSourceType.FullName)); } return(adapter); }
public void GetValue() { DataReaderDataSourceAdapter <IDataReader> adapter = new DataReaderDataSourceAdapter <IDataReader>(_reader); IDataReader reader = GetReaderAtRow(adapter, 1); object actual = adapter.GetValue(reader, new AdapterBoundField <IDataReader>("Name")); Assert.AreEqual("Mickey Mouse", actual); }
public void GetValueNull() { DataReaderDataSourceAdapter <IDataReader> adapter = new DataReaderDataSourceAdapter <IDataReader>(_reader); IDataReader reader = GetReaderAtRow(adapter, 2); object actual = adapter.GetValue(reader, new AdapterBoundField <IDataReader>("Name")); Assert.IsNull(actual); }
private IDataReader GetReaderAtRow(DataReaderDataSourceAdapter <IDataReader> adapter, int rowIndex) { IEnumerable enumerable = adapter.GetEnumerator(); Assert.IsNotNull(enumerable); IEnumerator enumerator = enumerable.GetEnumerator(); Assert.IsNotNull(enumerator); for (int i = 0; i < rowIndex; i++) { enumerator.MoveNext(); } return(enumerator.Current as IDataReader); }
public void GetEnumerator() { DataReaderDataSourceAdapter <IDataReader> adapter = new DataReaderDataSourceAdapter <IDataReader>(_reader); IEnumerable enumerable = adapter.GetEnumerator(); Assert.IsNotNull(enumerable); int i = 0; foreach (IDataReader reader in enumerable) { object[] row = GetRow(reader); CollectionAssert.AreEqual(_reader.Rows[i++], row); } Assert.AreEqual(4, i, "Should have read 4 rows."); }