Is there an example of the full builder pattern in the .NET base class library? I'm looking for something that has an actual director and multiple concrete builders.
Multiple concrete builders? Not that I know of unless you count WebRequest.Create
which is a pretty trivial kind of building.
ProcessStartInfo
and StringBuilder
are both reasonable as simple "one builder" examples though.
I'm far from an expert in this area, but I think that DbCommandBuilder and its inheriting classes (OdbcCommandBuilder, OleDbCommandBuilder, SqlCommandBuilder) might be an example...if it's OK that the abstract builder base class also serves as the director. The various concrete classes delegate to their base class, which then calls methods like these (per Reflector):
private DbCommand BuildDeleteCommand(DataTableMapping mappings, DataRow dataRow)
{
DbCommand command = this.InitializeCommand(this.DeleteCommand);
StringBuilder builder = new StringBuilder();
int parameterCount = 0;
builder.Append("DELETE FROM ");
builder.Append(this.QuotedBaseTableName);
parameterCount = this.BuildWhereClause(
mappings, dataRow, builder, command, parameterCount, false);
command.CommandText = builder.ToString();
RemoveExtraParameters(command, parameterCount);
this.DeleteCommand = command;
return command;
}
This fulfills some of the requirements to be a builder:
- abstract builder class
- concrete builder classes
- complex multi-step building in the various DbCommandBuilder methods (
BuildDeleteCommand
,BuildInsertCommand
,BuildUpdateCommand
) - product (
DbCommand
, cast to a specific command type by the concrete classes) which is more complex than just a SQL string, because it has a timeout, a command type, potentially a transaction, etc.
But there's not a separate class serving as the director; in effect, the DbCommandBuilder
base class is the director.