c# - 使用带有日期时间和 bool 值的自动映射器来表示 POCO 中的半天

标签 c# design-patterns asp.net-mvc-3 entity-framework-4 automapper

假设我有一个 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/

相关文章:

c# - 跟踪传递给外部 Web 服务的对象集合的模式?

c++ - 在静态成员分配之前调用函数

validation - 全局本地化验证

c# - Entity Framework 代码优先中的奇怪实体更新

c# - Datagridview 中的锚定复选框

C# 不允许发送或接收数据的请求,因为套接字未连接

reactjs - Ant Design + React-table。如何在 Ant Design 提供的 UI 元素之上构建 react-table 的过滤能力?

asp.net-mvc - 在 jQuery UI 对话框中显示 MVC3 Unobtrusive ValidationSummary 错误

c# - 使用 XElement 查询命名空间中的节点

c# - Unity IsPointerOverGameObject 问题