假设我有一个 POCO,它存储日期范围如下:
public class DateRange()
{
public DateTime FromDate{get;set;}
public DateTime ToDate{get;set;}
}
我希望能够在范围的开始和/或结束时节省半天的时间。为此,我将把我的数据显示为 jqueryui 日期选择器,用于表示起始日期和截止日期,以及每个日期选择器旁边的复选框来表示 1/2 天。我的 View 模型看起来像这样:
public class DateRangeModel()
{
public DateTime FromDate {get; set;}
public DateTime ToDate{get;set;}
public bool IsFromDateHalfDay{get; set;}
public bool IsToDateHalfDay {get; set;}
}
当为 FromDate 选中 1/2 天复选框时,我需要将实体中的 FromDate 保存为所选日期 + 12 小时,当为 ViewModel 中的 ToDate 选择 1/2 天复选框时,我需要将将实体中的 ToDate 保存为所选日期 + 12 小时。
是否可以使用 Automapper 来做到这一点?如果没有,您能建议更好/不同的方法吗?我不想让 bool 值代表实体中的半天,因为我想单独使用日期时间进行计算。
一些示例(仍在考虑如何表示 1 天和 1/2 天的范围 - 需要大量测试计算范围!):
A Date Range of 1/2 Day From 24th May to a full day 27th May:
2011-05-24 12:00:00.000 => 2011-05-28 00:00:00.000
A Date Range of Full Day From 24th May to a 1/2 day 27th May:
2011-05-24 00:00:00.000 => 2011-05-27 12:00:00.000
A Half Day on 24th May:
2011-05-24 12:00:00.000 => 2011-05-25 00:00:00.000
A Full Day on 24th May:
2011-05-24 12:00:00.000 > 2011-05-25 00:00:00.000
最佳答案
Jimmy Bogard 谈到在这里做类似的事情:http://lostechies.com/jimmybogard/2009/05/06/automapper-feature-custom-type-converters/
原则是您创建一个自定义映射。在映射时,您需要考虑这两个 bool 值,但它们不会出现在目标类型中。
关于c# - 使用带有日期时间和 bool 值的自动映射器来表示 POCO 中的半天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6348792/