.Net 4 中日期和两次的最佳方法/数据类型

发布于 2024-09-16 17:29:02 字数 741 浏览 7 评论 0原文

我有一个方法,它接受一些参数并生成一些 XML 以发送到[古老的] Web 服务。

我需要在 XML 中包含:

日期(年-月-日)

开放时间(时:分)

结束时间(时:分)

过去,当我必须单独提供日期/时间时,我会采用单个 DateTime 参数并格式化将日期放入一个字段,将时间放入另一字段。

在这种情况下,我有一个日期和一天中的两次。

我最初的方法是 3 个输入字段 - 一个日期,2 个时间跨度。这感觉不对,因为时间跨度可能超过一天。

另一个想法是采用 2 个日期 - 将一个日期用于日期字段,然后将两个日期的时间用于其他字段。这感觉不对,因为提供的日期实际上可能是不同的日子。

我能想到的最后一个选项是使用 OpeningDateTime 参数和 OpenDuration 时间跨度,但是,因为开盘/收盘时间是根据星期几(而不是日期)存储的,开发人员需要自己计算时间跨度 - 这看起来很愚蠢,因为它违反了 DRY 原则。更不用说时间跨度可以是> 1 天

简而言之,我有 3 个可行的解决方案,但没有一个感觉正确 - 并且前 2 个解决方案只会在运行时发现问题。如果方法定义使得传入无效数据很困难(如果不是不可能的话),那么我会更喜欢它 - 这将在设计时看到

也许我过度分析,但必须有一种更简洁的方法来做到这

一点任何人都有首选方法,如果有,为什么?

I've got a method which takes some parameters and generates some XML to be sent to an [archaic] web service.

I need to include in the XML:

Date (yyyy-mm-dd)

Opening time (hh:mm)

Closing time (hh:mm)

Now in the past when I've had to provide a date/time separately, I've taken in a single DateTime parameter and formatted the date into one field and the time into another.

In this scenario, I've got a single date and 2 times of day.

My initial approach was 3 input fields - one Date, 2 timespans. This feels wrong as the timespans can cover more than a day.

Another idea was to take 2 dates - use one for the date field and then the times from both for the other fields. This feels wrong as the dates provided may actually be different days.

The last option I can think of is having a OpeningDateTime parameter and an OpenDuration timespan, however, since the opening/closing times are stored against day of the week (not date), the developer would need to calculate the timespans themselves - which seems silly as it breaks DRY principles. Not to mention that again the timespan can be > 1 day

In short, I've got 3 solutions which would work but none of which feels right - and the first 2 solutions would only pick up problems at run-time. I'd prefer it if the method definition was such that passing in invalid data was difficult (if not impossible) - This would then be seen at design time

Maybe I'm over-analysing but there must be a neater way to do this

Does anyone have a preferred method and if so, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2024-09-23 17:29:02

真正想要的是一个具有分别表示“日期”和“时间”类型的库...例如Noda Time...但不幸的是,它还没有准备好用于生产:(基本上.NET没有一组非常丰富的时间类型:(

我认为你对 DateTime + 2 的最初想法TimeSpans 是目前最好的想法,事实上 DateTime.TimeOfDay 是一个 TimeSpan ,这表明这是您在正常方法中可以获得的最合适的表示形式。 。

What you really want is a library which has types representing a "date" and a "time" separately... like Noda Time... but unfortunately that's not ready for production yet :( Basically .NET doesn't have a very rich set of chronological types :(

I think your initial idea of DateTime + 2 TimeSpans is the best idea at the moment. In particular, the fact that DateTime.TimeOfDay is a TimeSpan suggests that's about as appropriate a representation as you can get within the normal approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文