AsInterface Method
The AsInterface method creates a live proxy for an IDataRow that implements
a POCO interface (TInterface). Getting and setting properties on the returned interface reads
and updates the underlying data row fields.
This enables the "programming to interfaces" approach: application code can work with strongly-typed POCO interfaces while still storing values inside a schema-driven, dynamic data set.
/// <summary>
/// Gets "live" data row as an interface
/// </summary>
/// <typeparam name="TInterface">POCO interface type</typeparam>
/// <param name="dataRow">Data row</param>
/// <returns>"Live" data row as an interface</returns>
public static TInterface AsInterface<TInterface>(this IDataRow? dataRow) where TInterface : class
/// <summary>
/// Gets "live" data row as an interface
/// </summary>
/// <typeparam name="TInterface">POCO interface type</typeparam>
/// <param name="dataRow">Data row</param>
/// <param name="nameMap">Name map</param>
/// <returns>"Live" data row as an interface</returns>
public static TInterface AsInterface<TInterface>(this IDataRow? dataRow, IDictionary<string, string> nameMap) where TInterface : class
Namespace: PocoDataSet.Extensions
Assembly: PocoDataSet.Extensions.dll
Assume you have a POCO interface that describes an employment type:
internal interface IEmploymentType
{
int Id { get; set; }
string Code { get; set; }
string? Description { get; set; }
}
Create a table, populate a row, then access the row through the interface:
// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Create an empty table from the POCO interface
IDataTable employmentTypeDataTable = dataSet.AddNewTableFromPocoInterface("EmploymentType", typeof(IEmploymentType));
// 3. Create a new row and set values
IDataRow employmentTypeDataRow = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow["Id"] = 1;
employmentTypeDataRow["Code"] = "ET01";
employmentTypeDataRow["Description"] = "Full Time";
// 4. Get a live proxy that implements IEmploymentType
IEmploymentType employmentType = employmentTypeDataRow.AsInterface<IEmploymentType>();
// 5. Change a property via the interface
employmentType.Code = "ET02";
// 6. Verify the underlying row has changed
string? updatedCode = employmentTypeDataRow["Code"] as string;
If your interface property names do not match column names, provide a name map:
// 1. Create an empty data set
IDataSet dataSet = DataSetFactory.CreateDataSet();
// 2. Create EmploymentType table
IDataTable employmentTypeDataTable = dataSet.AddNewTable("EmploymentType");
employmentTypeDataTable.AddColumn("EmploymentTypeId", DataTypeNames.INT32);
employmentTypeDataTable.AddColumn("EmploymentTypeCode", DataTypeNames.STRING);
employmentTypeDataTable.AddColumn("EmploymentTypeDescription", DataTypeNames.STRING);
// 3. Create a new row and set values
IDataRow employmentTypeDataRow = employmentTypeDataTable.AddNewRow();
employmentTypeDataRow["EmploymentTypeId"] = 1;
employmentTypeDataRow["EmploymentTypeCode"] = "ET01";
employmentTypeDataRow["EmploymentTypeDescription"] = "Full Time";
// 4. Create name map to map interface properties to data row field names
IDictionary<string, string> nameMap = new Dictionary<string, string>();
nameMap.Add("Id", "EmploymentTypeId");
nameMap.Add("Code", "EmploymentTypeCode");
nameMap.Add("Description", "EmploymentTypeDescription");
// 5. Get a live proxy that implements IEmploymentType
IEmploymentType employmentType = employmentTypeDataRow.AsInterface<IEmploymentType>(nameMap);
// 5. Change a property via the interface
employmentType.Code = "ET02";
// 6. Verify the underlying row has changed
string? updatedCode = employmentTypeDataRow["EmploymentTypeCode"] as string;
Table of Content POCO DataSet DataRow 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.