Validation of input arguments when creating new objects

Validation of input arguments when creating new objects

Old forum URL: forums.lhotka.net/forums/t/5327.aspx


mikemir8 posted on Friday, August 29, 2008

I have a business object which in the NewObject method receives another object as input parameter. This input parameter is used to set some of the properties of the new object.

Right now in this same method I'm validating that the input object is in an appropriate status before using it. If it is not, I throw an ArgumentException. The issue I have with this is that receiving an invalid object is very common, so it's not really an "exceptional" case. The other alternative I had was to create a business rule that would apply only when the business object is new, but in that case I would need to create a class variable to hold the input object, which right now I don't need to.

Other approach I consider is to leave it as it is, but implement a method that would allow the UI to test the validity of the input argument before trying to create the new object; Pretty much a Tester-Doer pattern.

I'd like to know what others are doing in this case. Any suggestions?

- Miguel

Fintanv replied on Friday, August 29, 2008

I have used a similar approach to the Tester-Doer pattern you describe.  I think it works well since you more than likely will want to provide some sort of UI communication to the user indicating the issue.

Copyright (c) Marimer LLC