Version 2.0 of the Framework introduces a factory model for creating instances of data access
objects. Using this factory enables you to write code that’s generic across different database
vendors. This feature is available through ADO, but to write vendor-neutral code in .NET
requires using an interface-based late bound programming model or one of the backwards
compatibility Managed Providers, such as OLEDB or ODBC. The interface-based approach
involves programming against the interfaces common to all Managed Providers, and then
using Reflection or a custom-written factory to actually load instances of types at runtime.
The backwards compatibility layer introduces a serious performance hit to your managed
applications. This is particularly painful if the databases you’re supporting do provide Man-aged Providers coded specifically for those vendors’ platforms. You don’t ever want to, for
example, use the OLEDB Managed Provider to talk to SQL Server 2000.
So in 2.0, there’s a Provider Factory model. This factory enables you to let configuration
entries drive what database you’re to use, but get instances of types from specific Managed
Providers at runtime. So if you’re using SQL Server, you get a SqlConnection object to use from
your code. If you’re using Oracle, you get an OracleConnection.
The services of the Provider Factory are exposed through a couple of types that Microsoft
added to the System.Data.Common namespace. DbProviderFactories exposes a couple of simple
shared methods (see Table 10-6).
Table 10-6. The Shared Methods of the DbProviderFactories type
Shared Method Meaning in Life
GetFactoryClasses This method returns a DataTable with metadata about all of the
installed providers on the system.
GetFactory This method returns an instance of a DbProviderFactory type. It
accepts an argument that describes the factory—either a DataRow from
the DataTable returned by GetFactoryClasses, or an “invariant name”
of the provider, which is a column returned in the DataTable.
All of the configured Managed Providers are available through the services of the Provider
Factory. To get a list of installed providers on a system, you can use the GetFactoryClasses
method on the DbProviderFactory type (see Default.aspxin Web10).
Running Codes
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.DataSource = DbProviderFactories.GetFactoryClasses();
gv.DataBind();
form1.Controls.Add(gv);
}











0 comments:
Post a Comment