Domanda

I am new to codeContracts,I like it because it seems clean.

I dont like the fact that I need to download a library to make it work.Never understood why microsoft didnt make it part of the framework as whole.

I dont want to impose other collegues in the team to download it.

Questions:

Can I download the extensions and add the dlls to the build so that if they get the latest or or work on it they have nothing to do?

Is there anything to know before deploying an app with Codecontracts inside?

thanks

È stato utile?

Soluzione

In .NET 4.0, you don't need to download or add any libraries to the project in order to be able to add code contracts to your code. The part that you download are the tools for static analysis and ccrewrite. If you are using an older version of .NET, I suggest you upgrade to 4.0 before starting to use Code Contracts.

As long as you use only the static analysis features (Static Contract Checking), you can use it without directly interfering with the development process of other developers (keep reading though). In this case nothing changes in terms of compilation or deployment.

If you were meaning to enable Perform Runtime Contract Checking, then you should get everyone on the team to use the tools. You should also make sure that your build server is able to produce the same results as your local Visual Studio. This is because as soon as you enable that feature, code contracts start to influence runtime behavior. Obviously, the behavior of the code must be the same for everyone.

Even if you decide to use only static features for now, there is a downside to not having everyone on the team using the tools: they won't be able to properly maintain the code that makes use of code contracts. Refactoring might require changes to the contracts, but without the tools they can inadvertently introduce a lot of warnings that you will have to fix if you're the only one with the tools.

That being said, I've worked with code contracts in specific parts of a shared code base without the rest of the team using them without any significant problems. Of course, you should still talk to your colleagues about it, so they won't run into any surprises when maintaining your code. The only times where my use of code contracts forces them to do some extra work, is when they add a method to an interface I've defined a contract for. The contract is in an abstract class implementing that interface, which means they will have to add a minimal implementation of that new method to the abstract class.

Altri suggerimenti

I've been using Code Contracts within a large development team for a few years now with limited take-up across the codebase.

Once you've worked out what you want to use from it (e.g. rewriting level / static analysis config etc) I would recommend uninstalling Code Contracts and instead checking in a copy of it into source control. This will make your code build on everyone's machines including the build server. It will also allow you to remove the "Contract reference assembly for project 'xxx' not found. Select 'Build' or 'DoNotBuild' for Contract Reference in project settings" warning without changing everyone else's projects. It will also reduce the risk of the rewriter not running because a builds been done without Code Contracts installed which can lead to warnings appearing at run-time. What you'll miss out on by doing this is the project configuration tab and the code editor enhancements (if they worked for you in the first place).

If you're using it on more than one project I would also recommend putting your standardised configuration in a targets file to share it between them (making the configuration tab a bit redundant).

If you decide to use static analysis I would have this turned off for your the normal configurations (i.e. Debug and Release) otherwise you'll potentially slow down builds for others (that first run can take a long time). I have a configuration specifically for running the static analysis so I can easily switch it between it when the results of the static analysis are needed.

BTW Our code is a mixture of .Net 2 and 4 which hasn't been a problem you just need to remember to reference the Microsoft.Contracts assembly for 2.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top