我应该做一个DATERANGE对象? [英] Should I make a DateRange object?

查看:121
本文介绍了我应该做一个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.

在我的经验,使得对象层次更深往往复杂的事情。这些对象将被发送到RDLC的ReportViewer控件显示的报告,但这是次要的。我会弯曲视图模型,而不是相反。我们不依赖于属性名,不过,愿意妥协的东西,如:

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?

侧面的问题:我错过了一个通用的通用的元组类首创置业的地方?我知道有一些非常具体的人在不同的命名空间中漂浮。污染我的公共领域的方法签名与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 您可能有兴趣 - 这当然使得简单的的DateTime 操作。谈到马克的回答,我不记得这是否是一个结构或类 - 你是受欢迎的改变,当然它

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.

这是很好,易步,由于马克的仿制药恶作剧(假设你使用.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.)

要解决马克的回答另外一点,野田佳彦时间就一定能恩$ PSS日期的概念,不是更恰当的.NET API p $,但我们没有像范围此刻什么...这是一个不错的主意,但 - 我已经添加了的feature申请

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天全站免登陆