I am creating a webservice using Windows Communication Foundation (WCF) and I currently don’t know what the best way to do validation with it is.
I have two methods: CreateCustomer(Customer) and CreateCustomers(List<Customer>).
If a client passes in a list of customers, and some of the customers are invalid, should I reject the entire request? Or should I return the ones that passed validation and label the ones that were invalid?
Or, should I only allow them to call the CreateCustomer(Customer) method and make them repeatedly call it if they want to create more than one customer?
In this type of situation, I would recommend a Transaction like approach.
Basically, you would validate all, if they don’t pass, throw the exception, or other validation failed events, with the customer(s) that did not pass using an identifier or the actual objects. This will allow the person on the other side of the pipeline to identify the issues.
For saving to the database, I would look into doing this in a transaction as well, partially saving 1-7 customers, but not the 8th could cause issues.