我在两个类之间映射数据,其中一个类是在另一个(销售订单)中创建或修改数据的采购订单。如果销售订单值不为空,我还会保留更改的事务日志。你能建议一种使这个通用的方法吗?
private static DateTime CheckForChange(this DateTime currentValue,
DateTime newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
private static decimal CheckForChange(this decimal currentValue,
decimal newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
private static int CheckForChange(this int currentValue,
int newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
原始建议代码示例
private static T CheckForChange<T>(this T currentValue, T newValue,
string propertyName) where T : ???
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
最终修订:
public static T CheckForChange<T>(this T currentValue, T newValue,
string propertyName, CustomerOrderLine customerOrderLine)
{
if (object.Equals(currentValue, newValue)) return currentValue;
//Since I am only logging the revisions the following line excludes Inserts
if (object.Equals(currentValue, default(T))) return newValue;
//Record Updates in Transaction Log
LogTransaction(customerOrderLine.CustOrderId,
customerOrderLine.LineNo,
propertyName,
string.Format("{0} was changed to {1}",currentValue, newValue)
);
return newValue;
}