3

Has anyone implemented the Enterprise Library VAB along wtih Code Contracts in .NET 4.0?

If so, can you share some insights? Did it help in performance? Any other factors to be considered?

Nick
  • 7,475
  • 18
  • 77
  • 128

1 Answers1

5

Both frameworks have a different scope so they could easily be used in the same project. However, you'll have to prevent using Code Contracts in your domain entities. When you do this, all callers must ensure that they don't set invalid values (a compile time error will occur when you do this). With Validation Application Block however, your entities must be allowed to have an (temporarily) invalid state. Otherwise VAB is unable to ever detect invalid objects.

Let me put it otherwise, Code Contracts is meant to prevent programming errors, not user errors and it gives compile time support for this. VAB prevents user errors, not programming errors* and gives runtime support for this.

*Okay, VAB could also be used for programming errors (and in fact, I do use it myself in this way) but main scenario is user input IMO.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Code Contracts does both compile-time and runtime checking (depending on which version of Visual Studio you are using). – adrianbanks Mar 19 '10 at 23:14
  • I didn't actually say that CC didn't give runtime support :-) The main point is that it checks against programming errors. – Steven Mar 22 '10 at 12:53
  • Thanks for the concise answer. How do you use VAB for preventing programming errors? – Nick Mar 23 '10 at 13:52
  • @user102533: When you have a process that runs automatically without any user input, you still want to validate all entities before sending them to the database. But when entities are considered invalid, the cause is a programming error, not invalid user input. Still, the process over validating those entities is exactly the same. – Steven Mar 23 '10 at 14:43
  • Do I understand correctly that you recommend to use VAB for any externally exposed methods(e.g. WCF operations and UI validation) and CodeContract for internal to project methods? – Michael Freidgeim Jun 03 '12 at 23:07
  • No, VAB is not for validating methods, but for validating the state of objects. – Steven Jun 04 '12 at 04:34
  • I expect that either validation checks values of parameters( i am in particular interested in wcf params validation). I am not sure, what you mean by "validating method". Could you clarify, when in the project user should use one or another technology? – Michael Freidgeim Jun 04 '12 at 12:33
  • To be more precise, you should use VAB for data validation (to guard against user input errors) and Code Contracts for contract validation (to guard against programming errors). Take a look at the ValidationHOL.pdf document (google 'ValidaitonHOL.pdf' and you will find it) and take a look at the WCF integration. It shows how to use VAB for validation WCF method arguments. – Steven Jun 04 '12 at 12:46