专门为表示Now的DateTime创建包装是一个好主意吗? [英] Is it an good idea to make a wrapper specifically for a DateTime that respresents Now?

查看:57
本文介绍了专门为表示Now的DateTime创建包装是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近注意到,使用模拟 now的DateTime作为方法的输入参数非常好,用于模拟和测试。而不是每个方法自己都调用 DateTime.UtcNow ,我在上层方法中执行一次,然后在下层方法中转发。

I have been noticing lately that is really nice to use a DateTime representing 'now' as an input parameter for your methods, for mocking and testing purposes. Instead of every method calling DateTime.UtcNow themselves, I do it once in the upper methods and forward it on the lower ones.

因此许多需要立即的方法都具有输入参数 DateTime now

So a lot of methods that need a 'now', have an input parameter DateTime now.

(我正在使用MVC,并尝试检测一个名为 now 的参数并将其与ModelDateDateTime.UtcNow绑定)

(I'm using MVC, and try to detect a parameter called now and modelbind DateTime.UtcNow to it)

所以,而不是:

public bool IsStarted
{
    get { return StartTime >= DateTime.UtcNow; }
}

我通常有:

public bool IsStarted(DateTime now)
{
    return StartTime >= now;
}

因此,如果一种方法的 DateTime 参数称为 now ,您必须将其与当前时间配合使用。当然,这取决于惯例,其他人可以轻松地在其中扔一些其他DateTime作为参数。

So my convention is at the moment, if a method has a DateTime parameter called now, you have to feed it with the current time. Of course this comes down to convention, and someone else can easily just throw some other DateTime in there as a parameter.

为了使它更加牢固和静态类型,我正在考虑将DateTime包装在新对象(即DateTimeNow)中。因此,在最高级的一层中,我将 DateTime 转换为 DateTimeNow ,当出现以下情况时,会出现编译错误:有人试图摆弄正常的DateTime。

To make it more solid and static-typed I am thinking about wrapping DateTime in a new object, i.e. DateTimeNow. So in one of the most upper layers I will convert the DateTime to a DateTimeNow and we will get compile errors when, someone tries to fiddle in a normal DateTime.

当然,您仍然可以解决此问题,但至少可以感觉到您在某些时候做错了什么。
还有其他人走过这条路吗?从长远来看,是否有我没有考虑过的好结果或坏结果?

Of course you can still workaround this, but at least if feels more that you are doing something wrong at point. Did anyone else ever went into this path? Are there any good or bad results on the long term that I am not thinking about?

推荐答案

您可以创建一个必要的接口属性。即 IClock 并将其作为依赖项注入。

You can create an interface with necessary properties. Namely IClock and inject it as a dependancy.

interface IClock
{
    DateTime Now { get; }
    DateTime UtcNow { get; }
}

class SystemClock : IClock
{
    public DateTime Now { get { return DateTime.Now; } }
    public DateTime UtcNow { get { return DateTime.UtcNow ; } }
}

class TestDoubleClock : IClock
{
    public DateTime Now { get { return whateverTime; } }
    public DateTime UtcNow { get { return whateverTime ; } }
}

通过这种方式,您可以轻松地对依赖于 DateTime 。将 DateTime.Now 作为参数传递到各处听起来很糟糕。如果您需要 Now 以及 UtcNow 和其他东西怎么办?您是否会为此单独添加三个参数?

This way you can easily unit test your code which depends on DateTime. Passing the DateTime.Now everywhere as parameter sounds crappy. What if you need Now and also UtcNow and something else? Will you add three parameter for this purpose alone?

我建议使用这种界面技术来避免包含太多参数的丑陋代码,而这些参数并不能为您提供太多帮助。

I suggest this interface technique to avoid ugly code with too many parameters which doesn't serves you much.

这篇关于专门为表示Now的DateTime创建包装是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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