为什么没有在.NET和日期时间没有IDateTimeProvider现已吸气剂? [英] Why there is no IDateTimeProvider in .NET and DateTime has Now getter?

查看:463
本文介绍了为什么没有在.NET和日期时间没有IDateTimeProvider现已吸气剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在写一个单元测试,做具体的日期时间验证的组件。我创建了 IDateTimeProvider 接口,充当 DateTime.UtcNow wraper和业务对象使用的界面,而不是的DateTime 直接。看来,日期时间有点超负荷,应分成价值和获取价值从操作系统的事情。我不知道是否有特殊原因不能在.NET中的 IDateTimeProvider IClock )的接口?

Currently I'm writing a unit test for a component that does datetime specific validation. I have created IDateTimeProvider interface, that serves as a DateTime.UtcNow wraper and business objects use interface rather than DateTime directly. It seems that DateTime is a bit overloaded and should be split into a value and a thing that gets that value from the OS. I wonder if there is a particular reason not to have a IDateTimeProvider (IClock) interface in .NET?

推荐答案

简单的说:因为首创置业的大部分地区并没有设计为可测试性

Simply put: because large parts of the BCL weren't designed for testability.

同样是真实的随机数生成,在核心功能方面 - 和大量的HTTP相关的类都差很多伪造出来:(至少在这种情况下,它是相当容易引入自己的时钟接口。

The same is true for random number generation, in terms of "core" functionality - and a lot of the HTTP-related classes are much worse to fake out :( At least in this case it's reasonably easy to introduce your own clock interface.

从有利的一面,当野田佳彦时间是准备生产使用,它不仅能提供更好的比BCL日期/时间API - 它会提供更多的测试友好之一:)

On the plus side, when Noda Time is ready for production use, it will not only provide a better date/time API than the BCL - it'll provide a more test-friendly one :)

这篇关于为什么没有在.NET和日期时间没有IDateTimeProvider现已吸气剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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