知乎周源微信_每周源代码6

知乎周源微信

知乎周源微信

In my new ongoing quest to read source code to be a better developer, I now present the sixth in an infinite number of a weekly series called "The Weekly Source Code." Here's some source I'm reading this week that I enjoyed.

在我不断追求阅读源代码以成为更好的开发人员的追求中,我现在以每周系列的无限数量(称为“每周源代码”)介绍第六个。 这是我本周喜欢的一些资料。

  • xUnit is a new Unit Testing Framework from Jim "Original NUnit Guy" Newkirk and Brad "The .NET Guy" Wilson. Posts about the "why" of it are already flying and I remain neutral, like Switzerland. Seems unnecessary to me, but whatever makes you happy. Let's see if there's anything neat in the source. You gotta give them style points for extensibility. See how they make up a new attribute "RepeatTest," give it behavior, then apply it to a test and the Test Runner just runs the test as the yield's return new TestCommands. Clean.

    xUnitJim“原始NUnit Guy” NewkirkBrad“ The .NET Guy” Wilson共同开发的新单元测试框架。 关于“为什么”的帖子已经在传播,我仍然保持中立,例如瑞士。 对我来说似乎是不必要的,但是任何使您快乐的事情。 让我们看看源代码中是否有任何整洁的东西。 您必须为它们提供样式点以实现可扩展性。 看看他们如何组成一个新的属性“ RepeatTest”,赋予其行为,然后将其应用于测试,测试运行器将在成品率返回新的TestCommands时运行该测试。 清洁。

    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's Password Manager (PWM) - I had my favorite Password Manager crash on launch for me today so I rebuilt it it locally and set the Platform to x86 and it worked. While I was in there...take a look at this bodiless "Record" constructor.

    Keith Brown的密码管理器(PWM) -今天,我在启动时遇到了我最喜欢的密​​码管理器崩溃,因此我在本地重建了它,并将Platform设置为x86并可以使用。 当我在那里的时候...看一下这个无体的“记录”构造函数。

    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 source) - Both Matt Gilbert and Mike Minutillo pointed me (back) to RhinoMocks Mike says he likes the DisposableAction pattern Ayende is fond of.

    RhinoMocks ( SVN来源)-Matt Gilbert和Mike Minutillo都向我指出了RhinoMocks(迈克),他说他喜欢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 - While you're there, take a look at these 70 lines of code. I wonder aloud if this leaks Assemblies, but it's OK because it's primarily created as a development speed thing. Clever though. Did YOU know how easy it is to compile new code from within .NET?

    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);
    }
    
    

Feel free to send me links to cool source that you find hasn't been given a good read.

随时向我发送指向很酷的资源的链接,您发现这些链接没有得到很好的阅读。

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-6

知乎周源微信

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值