AsInterface Method

Overview

The AsInterface method returns a live, strongly-typed view of a data row as a POCO interface. The returned object is a proxy over the underlying IDataRow, so changes made through the interface are immediately reflected in the row, and changes to the row are visible through the interface.

This overload is a table-level convenience wrapper. It validates the row index, retrieves the row from dataTable.Rows, and then delegates to DataRowExtensions.AsInterface<TInterface>.

Declaration

/// <summary>
/// Gets "live" data row as an interface
/// </summary>
/// <typeparam name="TInterface">POCO interface type</typeparam>
/// <param name="dataTable">Data table</param>
/// <param name="rowIndex">Row index</param>
/// <returns>"Live" data row as an interface</returns>
/// <exception cref="ArgumentOutOfRangeException">Exception is thrown if table does not have row with specified index</exception>
public static TInterface? AsInterface<TInterface>(this IDataTable? dataTable, int rowIndex) where TInterface : class

Namespace and Assembly

Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll

Behavior

Usage Example

Assume there is an interface describing an employment type:

/// <summary>
/// Defines employment type functionality
/// </summary>
internal interface IEmploymentType
{
  int Id { get; set; }
  string Code { get; set; }
  string? Description { get; set; }
}

You can obtain a live POCO interface over an existing row:

// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();

// 2. Create an empty data table from POCO interface
IDataTable employmentTypeDataTable = dataSet.AddNewTableFromPocoInterface("EmploymentType", typeof(IEmploymentType));

// 3. Add a row and set values
IDataRow employmentTypeDataRow = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow["Id"] = 1;
employmentTypeDataRow["Code"] = "ET01";
employmentTypeDataRow["Description"] = "Full Time";

// 4. Get data from data row using POCO interface (live projection)
IEmploymentType employmentType = employmentTypeDataTable.AsInterface<IEmploymentType>(0);

// 5. Change data through the interface
employmentType.Code = "ET02";

// 6. Verify that changes propagate to the underlying row
string? updatedCode = employmentTypeDataRow["Code"] as string;

Notes

 

Table of Content POCO DataSet DataTable Extensions Group DataTable Members

 


Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness or suitability of the information and are not responsible for any damages resulting from its use.