- 将所有内容映射到 POCO 对象。
- 编写一个(只读)get 属性……进行计算。
这是一个 ~basic~ ORM 映射器(又名,创建你的 POCO)
为什么 DataTable 比 DataReader 快
附加使用对象其他属性的只读(获取;仅)属性。
如果您的计算是“昂贵的”,并且您阅读了不止一次,则可以使用此“可空”技巧。
public class Employee
{
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
TimeSpan? _difference = null;
public TimeSpan Difference
{
get
{
TimeSpan returnValue;
if (this._difference.HasValue)
{
returnValue = this._difference.Value;
}
else
{
/* COSTLY CALCULATION HERE , ONE TIME */
this._difference = this.HireDate.Subtract(this.BirthDate);
/* End COSTLY Calculation */
returnValue = this._difference.Value;
}
return returnValue;
}
}
}