简体   繁体   中英

Overriding a property of an abstract class?

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

}

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.

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.

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM