Where do you perform your validation? <p>Hopefully you'll see the problem I'm describing in the scenario below. If it's not clear, please let me know.</p> <p>You've got an application that's broken into three layers, </p> <ul> <li>front end UI layer, could be asp.net webform, or window (used for editing Person data)</li> <li>middle tier business service layer, compiled into a dll (PersonServices)</li> <li>data access layer, compiled into a dll (PersonRepository)</li> </ul> <p>In my front end, I want to create a new Person object, set some properties, such as FirstName, LastName according to what has been entered in the UI by a user, and call PersonServices.AddPerson, passing the newly created Person. (AddPerson doesn't have to be static, this is just for simplicity, in any case the AddPerson will eventually call the Repository's AddPerson, which will then persist the data.)</p> <p>Now the part I'd like to hear your opinion on is validation. Somewhere along the line, that newly created Person needs to be validated. You can do it on the client side, which would be simple, but what if I wanted to validate the Person in my PersonServices.AddPerson method. This would ensure any person I want to save would be validated and removes any dependancy on the UI layer doing the work. Or maybe, validate both in UI and in by business server layer. Sounds good so far right? </p> <p>So, for simplicity, I'll update the PersonService.AddPerson method to perform the following validation checks - Check if FirstName and LastName are not empty - Ensure this new Person doesn't already exist in my repository</p> <p>And this method will return True if all validation passes and the Person is persisted, False if Validation fails or if the Person is not persisted.</p> <p>But this Boolean value that AddPerson returns isn't enough for me at the UI layer to give the user a clear reason why the save process failed. So what's a lonely developer to do? Ultimately, I'd like the AddPerson method to be able to ensure what its about to save is valid, and if not, be able to communicate the reasons why it's not invalid to my UI layer.</p> <p>Just to get your juices flowing, some ways of solving this could be: (Some of these solutions, in my opinion, suck, but I'm just putting them there so you get an understanding of what I'm trying to solve)</p> <ul> <li><p>Instead of AddPerson returning a boolean, it can return an int (i.e. 0 = Success, Non Zero equals failure and the number indicates the reason why it failed. </p></li> <li><p>In AddPerson, throw custom exceptions when validation fails. Each type of custom exception would have its own error message. In addition, each custom exception would be unique enough to catch in the UI layer</p></li> <li><p>Have AddPerson return some sort of custom class that would have properties indicating whether validation passed or failed, and if it did fail, what were the reasons</p></li> <li><p>Not sure if this can be done in VB or C#, but attach some sort of property to the Person and its underlying properties. This "attached" property could contain things like validation info </p></li> <li><p><strong>Insert your idea or pattern here</strong></p></li> <li><p><strong>And maybe another here</strong></p></li> </ul> <p>Apologies for the long winded question, but I definately like to hear your opinion on this. </p> <p>Thanks!</p>