【问题标题】:Overriding a property of an abstract class?覆盖抽象类的属性?
【发布时间】:2011-03-07 14:03:26
【问题描述】:

背景: 我将System.Data.Common.DBConnect 类用于一组连接到不同类型数据源(如 CSV、AD、SharePoint、SQL、Excel、SQL 等)的类。 有一个接口定义了所有数据源类型的契约。

我希望使用 DBConnection 对象的 connectionString 属性将文件路径存储在基于文件的源上,以传递给基于文件的数据源的 GetData(DBConnection conn) 方法。 这不起作用,因为在为 ConnectionStribg 属性分配字符串时会发生一些验证。 我的问题: 如何创建自己的从 DBConnection 类派生的类(它是一个抽象类),只添加一个名为 ParameterString 的属性?

tldr;我想从System.Data.Common.DBConnect 继承并添加我自己的字符串属性。怎么样?

编辑

界面如下:

public interface IDataImport
{
    DbConnection CreateDbConnection(params string[] connectionString);

    DataSet GetResults(DbConnection conn, params string[] strQuery);

    DataTable GetAvailableTables(DbConnection conn);

   DataTable GetAvailableFields(DbConnection conn, string tableName);

}

【问题讨论】:

    标签: c# inheritance overriding abstract-class


    【解决方案1】:

    您可以从 DBConnection 继承,但问题是您需要实现所有继承的抽象成员(其中有 22 个):

    public class MyConnect : DBConnection
    {
       public string FilePaths{ get; set; }
    
       //Still need to implement all of the 
    }
    

    我假设您实际上想要利用处理 DBConnection 实现的内置 ADO 类,所以这不是一个好的选择。

    也许您只需要单独跟踪信息。信息必须是连接类的一部分是否有特殊原因?

    你可以按照以下方式做一些事情:

    public class MyConnectionInfo
    {
       public DBConnection Connection { get; set; }
    
       public string FileNames { get; set; }  
    }
    

    这会将信息放在一起,但不会使 DBConnection 类的使用复杂化。

    【讨论】:

    • 类的接口将 DBConnection 指定为类中使用的方法的参数:请参阅顶部我的 Q 中的编辑。
    • 你拥有这些课程吗?如果是这样,你让他们接受你想要的任何东西。如果你不这样做,那么只需传入连接属性。
    • 我确实拥有类和接口,但我想为开发人员提供一个统一的接口,以便在我之后使用它,这就是为什么我使用接口来强制实现每个数据源的类方法。不过,我对其他建议持开放态度。
    【解决方案2】:

    DbConnection 类是抽象的,因此您必须实现它的所有抽象方法。看起来是这样的:

    protected override System.Data.Common.DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
    {
        throw new System.NotImplementedException();
    }
    
    public override string ConnectionString
    {
        get
        {
            throw new System.NotImplementedException();
        }
        set
        {
            throw new System.NotImplementedException();
        }
    }
    
    public override void ChangeDatabase(string databaseName)
    {
        throw new System.NotImplementedException();
    }
    
    public override void Close()
    {
        throw new System.NotImplementedException();
    }
    
    protected override System.Data.Common.DbCommand CreateDbCommand()
    {
        throw new System.NotImplementedException();
    }
    
    public override string DataSource
    {
        get { throw new System.NotImplementedException(); }
    }
    
    public override string Database
    {
        get { throw new System.NotImplementedException(); }
    }
    
    public override void Open()
    {
        throw new System.NotImplementedException();
    }
    
    public override string ServerVersion
    {
        get { throw new System.NotImplementedException(); }
    }
    
    public override System.Data.ConnectionState State
    {
        get { throw new System.NotImplementedException(); }
    }
    

    当然,您必须在下面抛出异常的每个方法中加入正确的逻辑。

    ConnectionString 属性为您提供了如何覆盖属性的示例。如果您需要其他属性,您可以像将任何其他属性添加到 C# 类一样添加它。

    【讨论】:

      【解决方案3】:

      让你的类也抽象...

      public abstract class MyClass:System.Data.Common.DBConnect 
      {
          abstract String ParameterString
          {
      
              get; set;
          }   
      }
      

      如果你不希望你的类是抽象的,那么要么从一个具体的类继承它,要么重写抽象方法。这里没有第三种选择...

      【讨论】:

      • 这将编译,但是发布者正在使用的连接的实现呢?他必须重写所有这些才能连接到数据源。
      【解决方案4】:

      感谢RQDQ:我终于用下面的类来做我需要的事情了:

      public class GenericConnection
      {
          public GenericConnection(){}
      
          public DbConnection DBConn { get; set; }
      
          public string Filename { get; set; }
      
      }
      

      如您所见,我添加了 System.Data.Common.DBConnect 作为属性,并添加了我需要的字符串属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-14
        • 2023-03-04
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多