Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Code Contracts
Message
De
21/08/2011 13:42:29
 
 
À
21/08/2011 13:03:47
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01521429
Message ID:
01521433
Vues:
45
>Basically (but oversimplified) a replacement for asserts or other parameter checking. For example you might currently have code such as (C# I'm afraid but you will get the jist):
public int ProcessInvoice(EdifactInvoice invoice)
>        {
>            if (invoice == null)
>                throw new NullReferenceException("Invoice cannot be null");
>
>            //Elided
>            return 5;
>        }
Using code contracts you can refactor this to:
public int ProcessInvoice(EdifactInvoice invoice)
>        {
>            Contract.Requires<NullReferenceException>(invoice != null);
>            Contract.Ensures(Contract.Result<int>() > 0);
>            //Elided
>            return 5;
>        }
The 'Contract.Requires' serves the same purpose as the original null check at runtime - but with versions higher than VS Pro will *also* catch any existing call to the method which might fail to fullfill the 'contract at compile time. The Contract.Ensures line will, in this non-realistic example, also throw an exception if the return value does not meet the condition. A neat thing with the latter is that it will do this even if there are multiple return paths.....
>
>Here's a link to an overview: http://research.microsoft.com/en-us/projects/contracts/

Interesting, thanks
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform