Factory pattern in the .NET BCL

The factory design pattern has been used by System.Data.Common to create an instance of the provider, connection, command, or adapter objects in order to fetch data from a relational database. The following code snippets demonstrates the idea:

 DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); DbConnection connection = factory.CreateConnection(); connection.ConnectionString = connectionString; string queryString = "SELECT CategoryName FROM Categories"; DbCommand command = factory.CreateCommand(); command.CommandText = queryString; command.Connection = connection; DbDataAdapter adapter = factory.CreateDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); adapter.Fill(table); ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.