.Net中的抽象工廠
l 我們說過,抽象工廠針對系列產品的應變。在使用ADO.NET進行數據訪問的時候,如果目標數據庫是Access,我們會使用OleDbConnection、OleDbCommand以及OleDbDataAdapter等一系列ADO.NET對象。那麼如果數據庫是SQl Server,我們又會改用SqlConnection、SqlCommand以及SqlDataAdapter等一系列ADO.Net對象。如果只使用一套對象,沒有什麼大問題,如果我們的數據訪問有系列變化的需求,比如可以針對Access和SQl Server,而且希望改換數據庫盡量對客戶端代碼透明,那麼就需要引入抽象工廠模式。
l 好在,ADO.Net 2.0中已經有了整套抽象工廠的類型。看下面的代碼,你應該能辨別這些類型在抽象工廠中的角色:
public abstract class DbProviderFactory
{
// Methods
protected DbProviderFactory()
{
}
public virtual DbCommand CreateCommand()
{
return null ;
}
public virtual DbCommandBuilder CreateCommandBuilder()
{
return null ;
}
public virtual DbConnection CreateConnection()
{
return null ;
}
public virtual DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return null ;
}
public virtual DbDataAdapter CreateDataAdapter()
{
return null ;
}
public virtual DbDataSourceEnumerator CreateDataSourceEnumerator()
{
return null ;
}
public virtual DbParameter CreateParameter()
{
return null ;
}
public virtual CodeAccessPermission CreatePermission(PermissionState state)
{
return null ;
}
// PropertIEs
public virtual bool CanCreateDataSourceEnumerator
{
get
{
return false;
}
}
}
public sealed class OleDbFactory : DbProviderFactory
{
// FIElds
public static readonly OleDbFactory Instance = new OleDbFactory();
// Methods
private OleDbFactory()
{
}
public override DbCommand CreateCommand()
{
return new OleDbCommand();
}
public override DbCommandBuilder CreateCommandBuilder()
{
return new OleDbCommandBuilder();
}
public override DbConnection CreateConnection()
{
return new OleDbConnection();
}
public override DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return new OleDbConnectionStringBuilder();
}
public override DbDataAdapter CreateDataAdapter()
{
return new OleDbDataAdapter();
}
public override DbParameter CreateParameter()
{
return new OleDbParameter();
}
public override CodeAccessPermission CreatePermission(PermissionState state)
{
return new OleDbPermission(state);
}
}
public sealed class SqlClIEntFactory : DbProviderFactory, IServiceProvider
{
// FIElds
public static readonly SqlClientFactory Instance = new SqlClIEntFactory();
// Methods
private SqlClIEntFactory()
{
}
public override DbCommand CreateCommand()
{
return new SqlCommand();
}
public override DbCommandBuilder CreateCommandBuilder()
{
return new SqlCommandBuilder();
}
public override DbConnection CreateConnection()
{
return new SqlConnection();
}
public override DbConnectionStringBuilder CreateConnectionStringBuilder()
{
return new SqlConnectionStringBuilder();
}
public override DbDataAdapter CreateDataAdapter()
{
return new SqlDataAdapter();
}
public override DbDataSourceEnumerator CreateDataSourceEnumerator()
{
return SqlDataSourceEnumerator.Instance;
}
public override DbParameter CreateParameter()
{
return new SqlParameter();
}
public override CodeAccessPermission CreatePermission(PermissionState state)
{
return new SqlClIEntPermission(state);
}
object IServiceProvider.GetService(Type serviceType)
{
object obj2 = null ;
if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type)
{
obj2 = GreenMethods.SystemDataSqlClIEntSqlProviderServices_Instance();
}
return obj2;
}
// PropertIEs
public override bool CanCreateDataSourceEnumerator
{
get
{
return true;
}
}
}