AsInterface Method
The AsInterface method returns a live, strongly-typed view of a data row
as a POCO interface. Any change made through the returned interface is immediately propagated
to the underlying IDataRow, and any change in the row is visible through the interface.
This method is a cornerstone of the POCO DataSet philosophy of programming to interfaces. It allows application code to work with domain-oriented interfaces instead of dictionary-style row access while still preserving full change tracking.
/// <summary>
/// Gets "live" data row as an interface
/// </summary>
/// <typeparam name="TInterface">POCO interface type</typeparam>
/// <param name="dataSet">Data set</param>
/// <param name="tableName">Table name</param>
/// <param name="rowIndex">Row index</param>
/// <returns>"Live" data row as an interface</returns>
/// <exception cref="KeyNotFoundException">Exception is thrown if dataset does not contains a table with specified name</exception>
/// <exception cref="ArgumentOutOfRangeException">Exception is thrown if table does not have row with specified index</exception>
public static TInterface? AsInterface<TInterface>(this IDataSet? dataSet, string tableName, int rowIndex) where TInterface : class
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
dataSet is null, the method returns default(TInterface).
IDataRow, created by
DataRowExtensions.AsInterface.
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; }
}
The interface can be used to read and modify row data directly:
// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Create a table from the POCO interface
IDataTable table = dataSet.AddNewTableFromPocoInterface("EmploymentType", typeof(IEmploymentType));
// 3. Add a row
IDataRow row = table.AddNewRow();
row["Id"] = 1;
row["Code"] = "ET01";
row["Description"] = "Full Time";
// 4. Access the row as a strongly-typed interface
IEmploymentType employmentType = dataSet.AsInterface<IEmploymentType>("EmploymentType", 0);
// 5. Modify data through the interface
employmentType.Code = "ET02";
// 6. Changes are reflected in the underlying row
string updatedCode = (string)row["Code"];
AsInterface does not create a copy of the data. It exposes a live projection over the row.
IDataTable.AddNewTableFromPocoInterface and
DataRowExtensions.AsInterface to enable fully interface-driven data access.
Table of Content POCO DataSet DataSet 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.