Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an example of the Builder Pattern in the .NET BCL?

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.

like image 566
Jonathan Allen Avatar asked Dec 17 '22 07:12

Jonathan Allen


1 Answers

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.

like image 167
Ryan Lundy Avatar answered Dec 19 '22 19:12

Ryan Lundy