重写抽象类的属性?

发布于 2024-10-20 10:28:08 字数 798 浏览 1 评论 0原文

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

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

太棒了;我想继承 System.Data.Common.DBConnect 并添加我自己的字符串属性。如何?

EDIT

界面如下:

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);

}

Background:
I am using the System.Data.Common.DBConnect class for a set of classes that connect to different types of data sources like CSV, AD, SharePoint, SQL, Excel, SQL etc.
There is an interface that defines the contract for all the Datasource types.

I wished to use the connectionString property of the DBConnection object to store file paths on the file-based sources to pass to the GetData(DBConnection conn) methods for the file-based data sources.
This does not work, as there is some validation happening when assigning a string the the ConnectionStribg proprty.
My question:
How do I create my own class derived from the DBConnection Class (it is an abstract class) that ONLY ADDS one property called ParameterString?

tldr; I want to inherit from System.Data.Common.DBConnect and add my own string property. How?

EDIT

The interface is as follows:

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海夜未眠 2024-10-27 10:28:08

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

public class MyConnect : DBConnection
{
   public string FilePaths{ get; set; }

   //Still need to implement all of the 
}

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

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

您可以执行以下操作:

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

   public string FileNames { get; set; }  
}

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

You can inherit from DBConnection, but the problem is you would need to implemented all of the inherited abstract members (of which there are 22):

public class MyConnect : DBConnection
{
   public string FilePaths{ get; set; }

   //Still need to implement all of the 
}

I'm assuming that you actually want to take advantage of the built in ADO classes that handle the implementation of DBConnection, so this is not a good option.

Perhaps you just need to keep track of the information separately. Is there a particular reason why the information has to be part of the connection class?

You could do something along the lines of:

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

   public string FileNames { get; set; }  
}

This would put the information together but not complicate the usage of the DBConnection class.

桃扇骨 2024-10-27 10:28:08

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# 类中,就像添加任何其他属性一样。

The DbConnection class is abstract, so you must implement all of its abstract methods. Here is what that looks like:

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(); }
}

Granted, you will have to put the correct logic in each of the methods that are throwing exceptions below.

The ConnectionString property gives you an example on how to override a property. If you need an additional property you can add it as you would any other property to a C# class.

悟红尘 2024-10-27 10:28:08

让你的类也抽象...

public abstract class MyClass:System.Data.Common.DBConnect 
{
    abstract String ParameterString
    {

        get; set;
    }   
}

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

Make your class abstract too...

public abstract class MyClass:System.Data.Common.DBConnect 
{
    abstract String ParameterString
    {

        get; set;
    }   
}

If you don't wish your class to be abstract, then either inherit it from a concrete class, or override abstract methods. No third option here...

当梦初醒 2024-10-27 10:28:08

感谢 RQDQ:我最终使用以下类来完成我需要的操作:

public class GenericConnection
{
    public GenericConnection(){}

    public DbConnection DBConn { get; set; }

    public string Filename { get; set; }

}

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

Thanks to RQDQ: I finally used the following class to do what I needed:

public class GenericConnection
{
    public GenericConnection(){}

    public DbConnection DBConn { get; set; }

    public string Filename { get; set; }

}

As you can see, I added the System.Data.Common.DBConnect as an attribute, and added the string attribute I needed too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文