每周源代码6
[原文发表地址] The Weekly Source Code 6
[原文发表时间] 2007-09-25 18:04
沿袭我一贯的要求,阅读源代码以开发出更好的程序,我现在为大家呈上不限数的每周系列“每周源代码”的第六篇,此后我也将继续附上其他的文章。以下是我这周在读的一些我很欣赏的源代码。
xUnit是出自“Original NUnit Guy”小组的Jim和“The .NET Guy”小组的Wilson之手的一个新的单元测试框架。关于讨论“为什么”创建xUnit的博文已经漫天都是,但我保持中立,就像瑞士的中立立场一样。我觉得没有必要,不过只要你们开心就好。我们来看看源代码中有什么好的地方。你不得不因他们代码的可扩展性给分。看看他们是怎么创建一个新的属性“RepeatTest”,赋予它行为,然后在测试时使用这个属性,于是测试人员只需执行测试因为会yield return新的TestCommands.Clean。
public class Example
{
static int val;
[RepeatTest(5, Timeout = 500)]
public void RepeatingTestMethod()
{
Thread.Sleep(100);
Assert.Equal(2, 2);
if (val == 0)
{
val++;
Thread.Sleep(1000);
}
}
}
public class RepeatTestAttribute : TestAttribute
{
readonly int repeatCount;
public RepeatTestAttribute(int repeatCount)
{
this.repeatCount = repeatCount;
}
public override IEnumerable<ITestCommand> CreateTestCommands(MethodInfo testMethod)
{
for (int index = 0; index < repeatCount; index++)
yield return new TestCommand(testMethod);
}
}
Keith Brown的密码管理工具(PWM)- 今天我最喜欢使用的密码管理工具启动时崩溃了,所以我在本地重新build了下,将Platform设置为x86,这样就好了。我在弄的时候…看看这个没有实体的“Record”构造函数。
public Record(string site, string salt, string encryptedUserId, string encryptedPassword, string encryptedNotes, string useSetWindowText, string duration, string nagSpan, string nextReminder, string lastReset, string usageCount)
: this(site, salt, encryptedUserId, encryptedPassword, encryptedNotes, "true" == useSetWindowText,
"" == duration ? 0 : Convert.ToInt32(duration),
"" == nagSpan ? 0 : Convert.ToInt32(nagSpan),
"" == nextReminder ? DateTime.MaxValue : Convert.ToDateTime(nextReminder),
"" == lastReset ? DateTime.Now : Convert.ToDateTime(lastReset),
"" == usageCount ? 0 : Convert.ToInt32(usageCount)) {
}
RhinoMocks(SVN源代码)- Matt Gilbert和Mike Minutillo都跟我推荐RhinoMocks。Mike说他喜欢这个Ayende深爱的DisposableAction模式。
namespace Rhino.Commons
{
public class DisposableAction<T> : IDisposable
{
Proc<T> _action;
T _val;
public DisposableAction(Proc<T> action, T val)
{
if (action == null)
throw new ArgumentNullException("action");
_action = action;
_val = val;
}
public T Value { get { return _val; } }
public void Dispose() { _action(_val); }
}
public class DisposableAction : IDisposable
{
Proc _action;
public DisposableAction(Proc action)
{
if (action == null)
throw new ArgumentNullException("action");
_action = action;
}
public void Dispose() { _action(); }
}
}
Monorail HotSwap – 如果你有兴趣,那就看一下这70行代码。我在想这会不会泄漏程序集,但因为它关注的是开发速度,所以应该不要紧。非常聪敏。你知道在.NET平台上编译新代码有多简单吗?
void CodeChanged(object sender, FileSystemEventArgs e)
{
string fileName = Path.GetFileNameWithoutExtension(e.FullPath);
string typeName = controllersNamespace+"."+fileName;
CompilerParameters options = CreateCompilerOptions();
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults compilerResults = provider
.CompileAssemblyFromFile(options, e.FullPath);
container.Kernel.RemoveComponent(typeName);
if(compilerResults.Errors.HasErrors)
return;
Type type = compilerResults.CompiledAssembly.GetType(typeName);
container.AddComponent(type.FullName, type);
}
如果你找到了还没有被人好好读过而又很棒的源代码,请随时告诉我。