Microsoft Developer Network >
Forums Home
>
Data Platform Development (Pre-release) Forums
>
WCF Data Services [formerly known as ADO.NET Data Services] (Pre-Release)
>
Validation
Validation
What is the best way for validation? Where to put it? On the serverside/clientside?
How and where to write them?
Say I have a field UnitPrice and this price cannot be less then 0...
All Replies
- Best way to validate is at all 3 layers...
1) presentation layer - when you accept input..
2) middle tier - where you invoke business method....
3)Database level - Before inserting or updating into Database through stored procedures...
Presentation layer validations are most important as you can restrict unecessary calls to middle tier and Database tier as the data is validated appropriately at Presentation layer itself..
You can use asp.net validation controls, or javascript validations for client side validations...
middle tier validation is done in C# or Vb.net code...
DB layer validation is done in SQL Server through stored procs or having right database integrity constraints on database like (PK, UK, FK, CHECK, etc..)
hope this helps...
Regards
Suds
p.s - please close the thread if it helps and vote as helpful. - Thank you for your ansver.
You wrote: 1) presentation layer - when you accept input..
Does this mean that you put some validationrules in the Reference.cs -file?
I tried this and it works. But when I refresh/update the server reference everything is gone and I have to start all over.
Could you be a bit more specific?
Say you have a
public partial class Customers
with
public string City
{
get
{
return this._City;
}
set
{
this.OnCityChanging(value);
this._City = value;
this.OnCityChanged();
}
}
private string _City;
partial void OnCityChanging(string value);
partial void OnCityChanged();
How to validate the City. Say, the City can't be empty... - The idea will be when you are accepting user input on a form (winform or asp.net page), you should validation at the control level to avoid unwanted data being sent to server. Taking the case of asp.net, you can validate at client side. However since someone may create a form request and post it as well, you should validate at server side.
Validation at all layers, as mentioned by Sudhanshu is usually the right way, but then it comes with some cost. Sometimes, when people are sure that the only way to call a specific business component is via their presentation layer, they tend to skip validation at business layer and similarly for data layer. but note that this makes your business and data layers less reusable as it relies on specific logic on presentation layer.
MVP Solution Architect

