code合同 - 不错,上的优势,但没有准备好黄金时间?黄金时间、优势、合同、code

由网友(南笙浅梦墨汐°)分享简介:我真是在.NET 4中(虽然DevLabs的帮助下)推出code合同迷住了。但有印刷精美的冷却我了不少。以下是这样说的:在目前没有解决方法时,后置条件在除了不使用它们线程安全的方法称为锁之外的问题。在.NET依靠二进制重写从而使构建慢。使用code合同也可能会产生一个运行时的性能损失。不能通过处理Contract...

我真是在.NET 4中(虽然DevLabs的帮助下)推出code合同迷住了。但有印刷精美的冷却我了不少。以下是这样说的:

在目前没有解决方法时,后置条件在除了不使用它们线程安全的方法称为锁之外的问题。 在.NET依靠二进制重写从而使构建慢。 使用code合同也可能会产生一个运行时的性能损失。 不能通过处理ContractFailed事件用于安全敏感的检查,因为它们可以在运行时被规避。

最大的对我来说是第一位的。我不知道是否有人写单线程应用程序了。因此,如果code合同并非能够支持多线程,我看不到太多使用它们。或者,也许我不应该被过分强调的,因为手术后置条件是断言方法本身,它可以是单元测试的内部。

顺便说一句,我还没有发现任何东西,我没有尝试拆卸我的code,看看preconditions注入。我在一个简单的方法,假设,当锁()先行,它是简单的后正确的,但在一个相当复杂的方法注入检查,当锁定在某处发生在中间,它可能是一个问题。或者,如果不是锁定其他一些其他机制()的使用。

解决方案   

当后置在除了不使用它们线程安全的方法称为锁之外目前还没有解决办法的问题。

不是一个真正的问题,如果你做一个锁声明线程安全的方法,你可以碰到一个问题,以及外部的检查自己。调整你对实行这个小'限制'。现在的问题是如何code重写要知道,你必须锁定的资源?我认为,这种限制会留在很长一段时间。

  

.NET依靠二进制重写从而构建慢

是的,有的IL必须进行评估,生成,并保存到注入一些IL建立新的依赖=更多的时间过程。

  

使用code合同也可能会产生一个运行时的性能损失。

也不稀奇还是个问题,更code(为code合同产生的)=慢。你只要有更多的code基本上,你可以通过代理类来运行的,以及因此有更多的code +可能的代理服务器。

  

不能处理ContractFailed事件可用于安全敏感的检查,因为它们可以在运行时被规避。

是不这样做。你甚至不用到disaasble /重新组装的组件跳过一个潜在的安全检查。在服务器上保留任何安全相关的控制流程(无论是Web或DB)如果可能的话,还是尽量模糊处理code到如果地方你最好的能力。

I got really captivated by code contracts introduced in .NET 4 (though with the help of DevLabs). But one fine print cooled me off quite a bit. Here is what it says:

There is currently no workaround the problem when postconditions are called outside of lock in a thread-safe method except for not using them. .NET relies on a binary rewriter thus making the build slower. Using code contracts may also incur a runtime performance hit. Can't be used for security-sensitive checks because they can be circumvented at runtime by handling the ContractFailed event. 刘欣,好样的

The biggest for me is the first one. I don't know if anyone write single-threaded apps anymore. So if code contracts are not able to support multi-threading, I don't see much use of them. Or maybe I shouldn't be stressed too much about that because postconditions are for asserting the internals of the method itself, which can be unit-tested.

BTW, I haven't found anything and I didn't try to disassemble my code to see where preconditions are injected. I suppose in a simple method when lock() goes first, it's simple to inject checks right after it but in a rather complicated method, when locking occurs somewhere in the middle, it may be an issue. Or if some other mechanisms other than lock() are used.

解决方案

There is currently no workaround the problem when postconditions are called outside of lock in a thread-safe method except for not using them.

Not really a problem—if you do a check yourself outside of a lock statement in thread-safe method you can run into a problem as well. Adjust your implemenation for this little 'limitation'. The question is how code rewriter going to know that you must be locking a resource? I think this limitation will stay on for a long time.

.NET relies on a binary rewriter thus making the build slower.

Yes, some IL has to be evaluated, generated, and saved to inject some IL build new dependencies = more time of course.

Using code contracts may also incur a runtime performance hit.

Also not unusual or a problem, more code (generated for the code contracts) = slower. You just have more code basically and what you may be running through a proxy class as well so there is more code + possible proxy.

Can't be used for security-sensitive checks because they can be circumvented at runtime by handling the ContractFailed event.

Yes don't do that. You don't even have to disaasble/reassemble the assembly to skip a potential security check. Keep any security related control flows on the server (be it web or db) if possible, or try to obfuscate your code to your best ability if local.

阅读全文

相关推荐

最新文章