问题描述
在过去的几个月里,我一直在为我的公司开发一个副项目,但高层现在认为它非常适合现有产品.
For the last few months I've been developing a side project for my company, but the higher-ups have now decided it would be a good fit in an existing product.
我一直在使用 Microsoft 的 Code Contracts 进行静态类型检查来开发副项目(部分原因是我之前没有使用过它们并且渴望学习).
I've been developing the side project using Microsoft's Code Contracts for static type checking (partly because I hadn't used them before and was eager to learn).
我的问题是,如果我将我的代码签入到代码库中,并且合同就位,其他所有开发人员是否都需要安装代码合同工具才能继续开发?我知道他们都没有安装它,而且我是这里的大三学生,所以我怀疑我能否说服他们所有人接受它.
My problem is that if I check in my code to the code base with Contracts in place, will every other developer need the Code Contracts tools installed to be able to continue developing? I know for a fact that none of them have it installed, and I'm the junior here so I doubt I could convince them all to take it up.
我使用的是 .Net 4.5,所以包含了代码契约库,但我想知道 Visual Studio 是否会抱怨他们没有使用在构建选项中指定的 CONTRACTS_FULL
进行构建当他们开始构建时,或者,如果我在构建选项中保留 CONTRACTS_FULL
,当另一个开发人员尝试构建时会发生什么?此外,我想知道当合约失败时最终产品将如何运作,但代码尚未使用代码合约重写器构建.
I'm using .Net 4.5, so the Code Contract libraries are included, but I'm wondering if Visual Studio will complain that they're not building with CONTRACTS_FULL
specified in the build options every time they go to build, or, if I leave CONTRACTS_FULL
in the build options, what will happen when another developer tries to build? Additionally I'm wondering how the end-product will act when a Contract fails, but the code has not been built with the Code Contracts Rewriter.
我只用一个项目创建了一个新解决方案.创建了一个触发代码合同违规的简单函数,代码合同已卸载且 CONTRACTS_FULL
未指定.构建并运行它并收到以下错误:
I created a new solution with just one project. Created a simple function that fired a code contract violation, with code contracts uninstalled and CONTRACTS_FULL
not specified. Built and ran it and received the following error:
Run-time exception (line 8): An assembly (probably "hdxticim") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires<TException> and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild. CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180.
After the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane. Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL
我认为错误消息需要重写,因为绝对没有指定 CONTRACTS_FULL.
I think the error message needs rewriting, as CONTRACTS_FULL is most definitely not specified.
多亏了 Matías Fidemraizer,我们发现在使用 Contract.Requires<TException>()
而不是 Contract.Requires()
时会发生这种情况.
Thanks to Matías Fidemraizer, we've worked out that this happens when using Contract.Requires<TException>()
and not on Contract.Requires()
.
理想情况下,我想修改此行为,以便合约触发提供的异常,就好像它是一个普通的保护语句一样,而不是抱怨重写器.
Ideally, I'd like to modify this behaviour so that the Contract fires the provided exception as if it were a normal guard statement instead of complaining about the rewriter.
Here's a fiddle 演示了这个问题:https://dotnetfiddle.net/cxrAPe
推荐答案
简短的回答是:是的.如果您使用代码契约签入代码,那么所有可能构建该代码的开发人员也必须安装代码契约才能构建代码.
The short answer is: Yes. If you check in the code with Code Contracts, then all developers that might build that code must also have Code Contracts installed in order to build the code.
与@CBauer 在他的回答中所写的相反,代码合同有一个祝福"包.不,它不是 NuGet 包——而是基于 MSI 安装程序的安装.
In contradiction to what @CBauer wrote in his answer, there is a "blessed" package for Code Contracts. No, it's not a NuGet package—it's an MSI installer based installation.
最后,如果您在持续集成环境中进行调试构建(例如开发、QA/QC 和/或测试),那么这些构建服务器也需要安装代码契约.
Finally, if you are in a Continuous Integration environment for debug builds (e.g. Development, QA/QC and/or Testing), then those build servers will also need to have Code Contracts installed.
当您使用代码契约时,调试版本总是需要使用代码契约.请注意,对于 Release 构建来说,不一定就是这种情况.这取决于您使用的合同检查形式以及项目属性中指定的选项.
When you use Code Contracts, Debug builds always require the use of Code Contracts. Please note that this is not necessarily the case for Release builds. It depends on what form of contract checking you're using and the options specified in the Project Properties.
代码契约手册包含所有详细信息.它非常好,我强烈建议您花时间阅读和理解它.
The Code Contracts manual has all the details. It's quite good and I highly recommend taking the time to read and understand it.
应该注意的是,如果您使用 Contract.Requires(bool condition)
形式的前提条件,您必须为 启用代码契约发布版本(请参阅第 5 部分:使用指南,特别是第 20 页,使用 2 方案).
It should be noted that if you use the Contract.Requires<TException>(bool condition)
form of preconditions, you must have Code Contracts enabled for Release builds (see Section 5: Usage Guidelines, specifically, page 20, Usage 2 scenario).
由于您正在将此代码集成到尚未使用代码合同开发的现有代码库中,您应该考虑修改您的代码合同项目属性设置以符合 中概述的使用场景 3代码合同手册的第 20 页,并使用传统"if-then-throw 模式重新制定您的合同.这将使您的团队能够最好地将代码库过渡到在任何地方使用代码契约,使您最终可以用实际的代码契约替换传统"if-then-throw 前提条件检查 Contract.Requires(bool condition)
检查,如果您愿意,Contract.Requires(bool condition)
检查.
Since you are integrating this code into an existing codebase that has not been developed with Code Contracts, you should consider revising your Code Contracts Project Property settings to conform to Usage Scenario 3 outlined on page 20 of the Code Contracts manual, and reformulate your contracts using the "legacy" if-then-throw pattern. This will enable your team to best tranisition the code base to using Code Contracts everywhere, allowing you to eventually replace "legacy" if-then-throw precondition checks with actual Code Contracts Contract.Requires(bool condition)
checks, and if you like, Contract.Requires<TException>(bool condition)
checks.
更新:很快就会有代码契约的 NuGet 包我今天在新的 GitHub 代码合约存储库.对于那些不知道的人,Microsoft 已将其开源,现在它是一项社区驱动的工作.
UPDATE: There Will Soon Be A NuGet Package For Code ContractsI was at the new GitHub repository for Code Contracts today. For those of you who don't know, Microsoft has open-sourced this and it is now a community-driven effort.
他们最近(早在 1 月)宣布发布 v1.10.xxxx.RC1.您可以在此处在他们的 GitHub 存储库上找到有关它的信息.
They recently (back in January) announced v1.10.xxxx.RC1 release. You can find information about it here on their GitHub repository.
这篇关于我可以在与非代码合同开发人员使用的代码库合并的代码中保留合同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!