Bagikan melalui


Factory Model Overview (ADO.NET)

ADO.NET 2.0 introduced new base classes in the System.Data.Common namespace. The base classes are abstract, which means that they can't be directly instantiated. They include DbConnection, DbCommand, and DbDataAdapter and are shared by the .NET Framework data providers, such as System.Data.SqlClient and System.Data.OleDb. The addition of base classes simplifies adding functionality to the .NET Framework data providers without having to create new interfaces.

ADO.NET 2.0 also introduced abstract base classes, which enable a developer to write generic data access code that does not depend on a specific data provider.

The Factory Design Pattern

The programming model for writing provider-independent code is based on the use of the "factory" design pattern, which uses a single API to access databases across multiple providers. This pattern is aptly named, as it calls for the use of a specialized object solely to create other objects, much like a real-world factory. For a more detailed description of the factory design pattern, see "Writing Generic Data Access Code in ASP.NET 2.0 and ADO.NET 2.0" and "Generic Coding with the ADO.NET 2.0 Base Classes and Factories" https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vsgenerics.asp on MSDN.

Starting with ADO.NET 2.0, the DbProviderFactories class provides static (or Shared in Visual Basic) methods for creating a DbProviderFactory instance. The instance then returns a correct strongly typed object based on provider information and the connection string supplied at run time.

See Also

Concepts

Obtaining a DbProviderFactory (ADO.NET)

DbConnection, DbCommand and DbException (ADO.NET)

Modifying Data with a DbDataAdapter (ADO.NET)

Other Resources

ADO.NET Managed Providers and DataSet Developer Center