我应该创建一个 DateRange 对象吗? [英] Should I make a DateRange object?

查看:33
本文介绍了我应该创建一个 DateRange 对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些域对象包含日期范围作为一对开始和结束日期属性:

A few of my domain objects contain date ranges as a pair of start and end date properties:

public class Period {
  public DateTime EffectiveDate { get; set; }
  public DateTime ThroughDate { get; set; }
}

public class Timeline {
  public DateTime StartDate { get; set; }
  public DateTime EndDate { get; set; }
}

我发现自己有很多这样的:

And I find myself with a lot of this:

abstract public int Foo(DateTime startDate, DateTime endDate);
abstract public decimal Bar(DateTime startDate, DateTime endDate);
abstract public ICollection<C5.Rec<DateTime, DateTime>> FooBar(DateTime startDate, DateTime endDate);

最后一个让我想知道......我应该实现一个 DateRange 类吗?我不知道 BCL 中有一个.

The last one made me wonder ... Should I implement a DateRange class? I'm not aware of one in the BCL.

根据我的经验,使对象层次结构更深通常会使事情复杂化.这些对象确实会发送到由 ReportViewer 控件显示的 RDLC 报告,但这是次要的.我会将视图弯曲到模型,而不是相反.不过,我们不依赖于属性名称,并且愿意妥协于以下内容:

In my experience, making the object hierarchy deeper often complicates things. These objects do get sent to RDLC reports displayed by the ReportViewer control, but that's secondary. I'll bend the view to the model rather than vice versa. We aren't tied to the property names, though, and would be willing to compromise with something like:

public class DateRange {
  public DateTime StartDate { get; set; }
  public DateTime EndDate { get; set; }
}

Period p = new Period();
DateTime t = p.EffectiveDateRange.StartDate;

DateRange 类的一个好处是可以集中验证开始日期之后的结束日期,这将简化我的方法签名:

A benefit of a DateRange class would be centralized validation of the end date coming after the start date, and it will simplify my method signatures:

abstract public int Foo(DateRange dateRange);
abstract public decimal Bar(DateRange dateRange);
abstract public ICollection<DateRange> FooBar(DateRange dateRange);

我只是不确定 DateRange 类不会给我带来比其价值更多的麻烦.意见?

I'm just not sure that a DateRange class won't get me into more trouble than its worth. Opinions?

附带问题:我是否遗漏了 BCL 中某个通用的通用元组类?我知道在不同的命名空间中有一些非常具体的内容.用 C5 类型污染我的公共领域方法签名感觉非常非常脏.

Side question: Did I miss a generic general-purpose tuple class in the BCL somewhere? I know there's some very specific ones floating around in various namespaces. Polluting my public domain method signatures with C5 types feels very, very dirty.

推荐答案

不,您没有错过通用课程.

No, you didn't miss a general purpose class.

我在 MiscUtil 中有一个 Range 类型,你可以感兴趣 - 它当然可以实现简单的 DateTime 操作.参考 Marc 的回答,我不记得这是一个结构体还是一个类——当然欢迎你改变它.

I have a Range type in MiscUtil which you may be interested in - and it certainly makes for simple DateTime manipulation. Referring to Marc's answer, I can't remember whether this is a struct or a class - you'd be welcome to change it of course.

由于 Marc 的泛型恶作剧(假设您使用的是 .NET 3.5,至少 - 2.0 是可行的,但目前不支持),这很好而且很容易通过;

It's nice and easy to step through, due to Marc's generics shenanigans (assuming you're using .NET 3.5, at least - it's feasible with 2.0 but not supported at the moment);

Range<DateTime> range = 19.June(1976).To(DateTime.Today);

foreach (DateTime date in range.Step(1.Days())
{
    // I was alive in this day
}

(这也使用了一堆扩展方法 - 对于测试比生产更有用.)

(That's also using a bunch of extension methods - more useful for test than production.)

为了解决 Marc 回答中的另一点,Noda Time 肯定能够比 .NET API 更恰当地表达日期的概念,但我们目前没有任何类似范围的东西......虽然这是一个好主意 - 我添加了一个 功能请求.

To address the other point in Marc's answer, Noda Time will certainly be able to express the concept of a date more appropriately than the .NET API, but we don't have anything like a range at the moment... It's a nice idea though - I've added a feature request.

这篇关于我应该创建一个 DateRange 对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆