出于测试目的,我需要IEnumerable<KeyValuePair<string, string>>使用以下示例键值对创建对象:
Key = Name | Value : John
Key = City | Value : NY
Run Code Online (Sandbox Code Playgroud)
这样做最简单的方法是什么?
我对CacheItemPolicy上的AbsoluteExpiration属性感到困惑.
它的MSDN文档说"在逐出缓存条目之前必须经过的时间段." 它使用System.DateTimeOffset来定义"时间段".
但是,如果你看看DateTimeOffset的MSDN文档,它会说它"代表一个时间点...相对于协调世界时(UTC)." 另请参阅此StackOverflow线程.
你看到了问题吗?AbsoluteExpiration期望"时间段"(例如5秒或2小时),但它需要一个代表"时间点"的对象(如美国东部时间2012年12月21日06:14:00).
在下面的代码中,我为所有项目定义了一个策略.我希望每个项目cacheExpiryInSeconds在添加后都会过期几秒钟.有人能证实我这样做是正确的吗?
public class MyCache : IRoutingInfoCache
{
MemoryCache _routingInfoCache;
CacheItemPolicy _cachePolicy;
public MyCache(int cacheExpiryInSeconds)
{
_routingInfoCache = new MemoryCache("myCache");
_cachePolicy = new CacheItemPolicy() {
AbsoluteExpiration =
new DateTimeOffset(
DateTime.UtcNow.AddSeconds(cacheExpiryInSeconds))
};
}
public void Put(string key, object cacheItem)
{
// based on how I constructed _cachePolicy, will this item expire
// in cacheExpiryInSeconds seconds?
_routingInfoCache.Add(new CacheItem(key, cacheItem), _cachePolicy);
}
}
Run Code Online (Sandbox Code Playgroud) 这里有一个类似的问题:没有从〜/ .gradle/gradle.properties读取Gradle属性,但它没有解决我的问题.
在我看来,gradle不是在阅读我的~/.gradle/gradle.properties文件.
我有一个gradle.properties文件~/.gradle,它具有在上传到maven central之前签署工件所需的属性.它看起来像这样:
signing.keyId=12345678
signing.password=myPassword
signing.secretKeyRingFile=/home/me/.gnupg/secring.gpg
sonatypeUsername=me
sonatypePassword=myOtherPassword
Run Code Online (Sandbox Code Playgroud)
当我尝试构建我的项目时,它抱怨没有sonatypeUsername属性,因此:
> Could not find property 'sonatypeUsername' on root project 'yourProject'.
Run Code Online (Sandbox Code Playgroud)
这是我的项目build.gradle的相关部分:
uploadArchives {
repositories {
mavenDeployer {
// lots of non-interesting things here
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: project.property("sonatypeUsername"), password: project.property("sonatypePassword"))
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用调试构建项目时,这是我看到的有关属性的内容:
$ ./gradlew --stacktrace --debug build
[INFO] [o.g.BuildLogger] Starting Build
[DEBUG] [o.g.BuildLogger] Gradle user home: /home/me
[DEBUG] [o.g.BuildLogger] Current dir: /home/me/dev/yourProject
[DEBUG] [o.g.BuildLogger] Settings file: null
[DEBUG] [o.g.BuildLogger] Build …Run Code Online (Sandbox Code Playgroud) 我以为我会尝试在命令行上模拟ATM机的代码kata.我决定使用TDD来推动设计.我遇到了一个有趣的场景,我很好奇其他人在这个场景中所做的事情.
如果你在我的AtmMachine课上看(在底部),你会注意到我正在故意退出我的while循环,这样我的测试就不会超时.这对我来说就像一个代码味道,我想知道其他人是否做过这样的事情.
我目前的感受是分开的:
以下是迄今为止我为atm机器进行的单元测试:
[TestClass]
public class when_atm_starts
{
private static readonly string WELCOME_MSG = "Welcome to Al Banco de Ruiz!";
private AtmMachine _atm;
private Mock<IAtmInput> _inputMock;
private Mock<IAtmOutput> _outputMock;
private Mock<ILogger> _loggerMock;
private Mock<ICommandFactory> _cmdFactoryMock;
[TestInitialize]
public void BeforeEachTest()
{
_inputMock = new Mock<IAtmInput>();
_outputMock = new Mock<IAtmOutput>();
_loggerMock = new Mock<ILogger>();
_cmdFactoryMock = new Mock<ICommandFactory>();
_atm = new AtmMachine(_inputMock.Object, _outputMock.Object, _loggerMock.Object, _cmdFactoryMock.Object);
}
[TestMethod]
public void no_one_should_be_logged_in()
{
this.SetupForCancelledUser();
_atm.Start();
Assert.IsNull(_atm.CurrentUser);
}
[TestMethod]
public void should_print_welcome_to_output() …Run Code Online (Sandbox Code Playgroud) ReSharper在方法调用中自动完成开括号,我喜欢这样.我希望它继续这样做.然而!是否有一些简单的方法可以将插入符号移过自动完成的括号?
例如,当我输入:
Encoding.UTF8.GetBytes(
我立即给出了右括号:
Encoding.UTF8.GetBytes()
然后我添加"Test"作为参数:
Encoding.UTF8.GetBytes("Test")
但现在,我的插入符号紧接在右括号之前.把手移到END钥匙或箭头上是很烦人的,所以我可以跳过自动完成的括号,这样我就可以添加一个分号:
Encoding.UTF8.GetBytes("Test");
既不TAB也不ENTER做我想要的.这里给出了答案:如何使用Resharper在Visual Studio中快速移动到生成的大括号/括号/等的末尾?建议我继续打字).但是,如果我要输入它,那么自动关闭括号的意义是什么?
假设我有一些 .NET 代码......
public class EventEnvelope
{
public Dictionary<string, string> Headers { get; set; }
public byte[] Body { get; set; }
}
public class EventSelector
{
public Predicate<Dictionary<string, string>> Selector { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想将此事件选择器发送到一个不是用 .NET 编写的事件代理。
我如何以跨平台的方式序列化这个谓词,以便用另一种语言编写的程序可以重建谓词并执行它?
我曾想过尝试编写一个序列化程序,它将谓词写出一些 javascript,然后让 rhino 或 jint 解释并在代理处执行它。
或者也许有一种实际的跨平台方式来表示我不知道的谓词?
你怎么认为?
.net ×4
c# ×2
build.gradle ×1
caching ×1
gradle ×1
jint ×1
predicate ×1
resharper ×1
tdd ×1
unit-testing ×1