views:

840

answers:

2
+6  A: 

SqlParameterOption's methods can all be instance methods that returns the same object:

class SqlParameterOption
 {
    public SqlParameterOption Precision(int p) {/* ... */; return this;}
    public SqlParameterOption Substitute() {/* ... */; return this;}
    /* ... */       
 }

/* ... */
SqlParameter.Int32(":ID", 1234).With(new SqlParameterOption()
                                           .Precision(15)
                                           .Substitute());

Re: building up state to be applied later vs. applying directly with each call, if there's no real irreverisible side-effects in either case, then it doesn't matter and it's up to your personal taste. If the options are commited with each method call and there's a chance you might want to undo that, then you might want to build up the state first and then apply it. If the parameter object does validation between properties for you as you apply them then it might be better to go with direct application so you'll get validation feedback right way.

Mark Cidade
+1  A: 

You can have overloaded methods though. For example, if it was Substitute(). You can't normally have both static and instance versions of a method, but extension methods might be of some use... but if the two versions of Substitute have different meanings, it would be cleaner to simply have different types being returned, so that the two variants of Substitute() can't conflict.

Marc Gravell
In this case there would be no conflict about meaning, the question was more about how I organize the code to get what I want, syntax-wise. Newing up an instance of the option object solves that problem, as shown by marxidad.
Lasse V. Karlsen
Fair enough - marxidad already has my +1 ;-p
Marc Gravell