Simon Vandemoortele wrote:

> I would like to go for [1] since it is very simple but in many cases it
> just shifts the complexity to the client since the client now has to
> buffer data in a structure that probably looks a lot like Contact until
> it has enough to create a valid contact. 
> So I guess I should use [2] but I don't know how to do that ... do I
> create two classes of objects: IncompleteContact and Contact. Generate
> the second from the first through validation ?

How about trying both ways and seeing what happens: I suspect you're 
perhaps overthinking this problem...

FWIW, I think there are two forms of validity you need to consider: 
object-level and persistence-level. Objects must always maintain their 
class invariants, but these invariants can be laxer than those required 
by the persistence layer. For example, persistence may insist on a 
unique, non-blank name, while the object representation may not care.

For a while I had validation performed by my save method. However I 
started finding I needed more flexibility: in particularly I often 
needed to validate but not store. So now I have two methods. Here's an 
actual routine that captures details of sales parameters from a web page 
and validates them. It saves the business object away if correct, 
otherwise it reports the errors. (The @data stuff is my session 
persistence interface, and the hash business lets me transfer fields 
back and forth by name in a lightweight way)

   def handle_maintain_sales
     sp = @data.sp
     values = hash_from_cgi
     sp.from_hash(values)
     errs = sp.error_list     # this is the validation call, returning
     if errs.empty?           # an array of errors
       sp.save
       note "Parameters updated"
       @session.pop
     else
       error_list(errs)
       maintain_again
     end
   end


Cheers



Dave