我应该使用 CodeContract 还是 CuttingEdge.Conditions?
我研究了使用条件框架来验证数据,而不是
if(cond) throw new SomeException();
SomeFramework.MakeSure(cond);
最后我的选择是使用 CodeContract
或 CuttingEdge.Conditions
框架。
我无法决定使用哪个框架。我可以告诉你,我不喜欢“CodeContract”框架的是,你必须安装额外的 msi
才能使用它以及你需要选择的选项;并不是说它不好,而是感觉不自然。 (当然,它仍在 MS 研究中。)
你觉得怎么样?
I researched the use of a condition framework to verify data instead of
if(cond) throw new SomeException();
SomeFramework.MakeSure(cond);
In the end my choice is to use either the CodeContract
or CuttingEdge.Conditions
frameworks.
I can not decide which framework to use. I can tell you that what I don't like about the 'CodeContract' framework is that you have to install the extra msi
in order to use it and the options you need to choose; not that its bad, but it feels not natural. (And of course its still under MS research.)
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CodeContracts
框架是 .NET 4 的一部分。因此您可以根据它编写代码,而无需安装任何东西,只是如果没有重写器组件,代码契约在运行时不会产生任何效果。我将这种包含在框架中的行为视为微软打算在未来更多地集成代码契约的标志。根据
CuttingEdge.Conditions
CodePlex 页面 上的统计数据,仅下载了 4,189 次次。关于语法有一些不错的东西,但除非有CuttingEdge.Conditions
特别支持的东西,而不是CodeContracts
,否则您最好坚持使用属于。网。就我而言,代码契约的主要功能如下:
您可以在接口上设置代码契约,以指定实现这些接口的类型的预期行为。
代码契约是继承的。
我还没有尝试过
CuttingEdge.Conditions
,但它并不明显支持这两个功能(而CodeContracts
则支持)。The
CodeContracts
framework is part of .NET 4. So you can write code against it without having to install anything, it's just that without the rewriter component the code contracts won't have any effect at runtime. I take this inclusion in the framework as a sign that Microsoft intends to integrate code contracts more in future.According to the stats on the
CuttingEdge.Conditions
CodePlex page, it's only been downloaded 4,189 times. There are some nice things about the syntax, but unless there is something specifically supported byCuttingEdge.Conditions
and not byCodeContracts
, you might as well stick with the version that's part of .NET.The key features of code contracts, as far as I am concerned, are as follows:
You can set up code contracts on interfaces, to specify the expected behaviour of types implementing those interfaces.
Code contracts are inherited.
I haven't tried
CuttingEdge.Conditions
, but it's not obvious that it supports these two features (whereasCodeContracts
does).主要区别在于代码合约包含静态检查器。这意味着您的合同将在编译时检查其正确性。
此外,只要您是针对 .NET 4 进行构建,您的用户就无需安装任何内容。重写器在编译时工作,CC 的其余部分是 .NET 的一部分。
The main difference is that Code Contracts includes a static checker. This means your contracts will be checked at compile time for correctness.
Also, as long as you are building for .NET 4, your users don't need to install anything. The rewriter works at compile time and the rest of CC is part of .NET.
编辑:我建议人们使用https://github.com/adamralph/liteguard
CuttingEdge.Conditions 已被分叉为“Conditions”。原作者不再维护或使用该项目:https://conditions.codeplex.com/workitem/ 20064
CodeContracts 未在 Mono 中实现。有一个 GSOC 项目,但它最终没有提供完整的解决方案,因此如果针对 Xamarin.iOS/Xamarin.Android/Xamarin.Mac 平台或一般来说只是单声道,则 Conditions 是您唯一的选择。
该库现在是一个可移植类库,默认情况下跨平台支持位于:
https://github.com/ ghuntley/Conditions 和 https://www.nuget.org/packages/Conditions/
Edit: I recommend people use https://github.com/adamralph/liteguard
CuttingEdge.Conditions has been forked to become just Conditions. The original author was no longer maintaining nor uses the project: https://conditions.codeplex.com/workitem/20064
CodeContracts are not implemented in Mono. There was a GSOC project but it didn't end up with a complete solution so Conditions is your only choice if targeting Xamarin.iOS/Xamarin.Android/Xamarin.Mac platforms or just mono in general.
The library is now a portable class library and cross-platform support by default is in:
https://github.com/ghuntley/Conditions and https://www.nuget.org/packages/Conditions/