l 我们说过,抽象工厂针对系列产品的应变。在使用ADO.NET进行数据访问的时候,如果目标数据库是Access,我们会使用OleDbConnection、OleDbCommand以及OleDbDataAdapter等一系列ADO.NET对象。那么如果数据库是SQL Server,我们又会改用SqlConnection、SqlCommand以及SqlDataAdapter等一系列ADO.NET对象。如果只使用一套对象,没有什么大问题,如果我们的数据访问有系列变化的需求,比如可以针对Access和SQL Server,而且希望改换数据库尽量对客户端代码透明,那么就需要引入抽象工厂模式。
l 好在,ADO.NET 2.0中已经有了整套抽象工厂的类型。看下面的代码,你应该能辨别这些类型在抽象工厂中的角色:
publicabstractclassDbProviderFactory { // Methods protected DbProviderFactory() { } publicvirtual DbCommand CreateCommand() { returnnull; } publicvirtual DbCommandBuilder CreateCommandBuilder() { returnnull; } publicvirtual DbConnection CreateConnection() { returnnull; } publicvirtual DbConnectionStringBuilder CreateConnectionStringBuilder() { returnnull; } publicvirtual DbDataAdapter CreateDataAdapter() { returnnull; } publicvirtual DbDataSourceEnumerator CreateDataSourceEnumerator() { returnnull; } publicvirtual DbParameter CreateParameter() { returnnull; } publicvirtual CodeAccessPermission CreatePermission(PermissionState state) { returnnull; } // Properties publicvirtualbool CanCreateDataSourceEnumerator { get { returnfalse; } } } publicsealedclassOleDbFactory : DbProviderFactory { // Fields publicstaticreadonlyOleDbFactory Instance = newOleDbFactory(); // Methods private OleDbFactory() { } publicoverride DbCommand CreateCommand() { returnnewOleDbCommand(); } publicoverride DbCommandBuilder CreateCommandBuilder() { returnnewOleDbCommandBuilder(); } publicoverride DbConnection CreateConnection() { returnnewOleDbConnection(); } publicoverride DbConnectionStringBuilder CreateConnectionStringBuilder() { returnnewOleDbConnectionStringBuilder(); } publicoverride DbDataAdapter CreateDataAdapter() { returnnewOleDbDataAdapter(); } publicoverride DbParameter CreateParameter() { returnnewOleDbParameter(); } publicoverride CodeAccessPermission CreatePermission(PermissionState state) { returnnewOleDbPermission(state); } } publicsealedclassSqlClientFactory : DbProviderFactory, IServiceProvider { // Fields publicstaticreadonlySqlClientFactory Instance = newSqlClientFactory(); // Methods private SqlClientFactory() { } publicoverride DbCommand CreateCommand() { returnnewSqlCommand(); } publicoverride DbCommandBuilder CreateCommandBuilder() { returnnewSqlCommandBuilder(); } publicoverride DbConnection CreateConnection() { returnnewSqlConnection(); } publicoverride DbConnectionStringBuilder CreateConnectionStringBuilder() { returnnewSqlConnectionStringBuilder(); } publicoverride DbDataAdapter CreateDataAdapter() { returnnewSqlDataAdapter(); } publicoverride DbDataSourceEnumerator CreateDataSourceEnumerator() { return SqlDataSourceEnumerator.Instance; } publicoverride DbParameter CreateParameter() { returnnewSqlParameter(); } publicoverride CodeAccessPermission CreatePermission(PermissionState state) { returnnewSqlClientPermission(state); } objectIServiceProvider.GetService(Type serviceType) { object obj2 = null; if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type) { obj2 = GreenMethods.SystemDataSqlClientSqlProviderServices_Instance(); } return obj2; } // Properties publicoverridebool CanCreateDataSourceEnumerator { get { returntrue; } } } |