Is there an example Builder pattern in .NET BCL?

Is there an example of a complete builder pattern in a .NET base class library? I am looking for what has an actual director and several specific builders.

+4
source share
2 answers

I am far from an expert in this field, but I think that DbCommandBuilder and its inheriting classes ( OdbcCommandBuilder , OleDbCommandBuilder , SqlCommandBuilder ) can be an example ... if it is so, that the base class of the abstract builder also acts as a director. Various concrete classes delegate them to the base class, which then calls such methods (for the 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 requirements for the builder:

  • abstract constructor class
  • concrete builder classes
  • complex multi-stage construction in various methods of DbCommandBuilder ( BuildDeleteCommand , BuildInsertCommand , BuildUpdateCommand )
  • ( DbCommand is discarded by a specific command type by specific classes), which is more complex than just an SQL string, because it has a timeout, command type, potentially a transaction, etc.

But there is no separate class acting as a director; in fact, the base class of DbCommandBuilder is a director.

+10
source

A few concrete collectors? Not that I know, unless you count WebRequest.Create , which is a pretty trivial kind of building.

ProcessStartInfo and StringBuilder are both reasonable, as are simple "one-builder" examples.

+2
source

All Articles