Solved

Change return type in Interface Method of a Class (C#)

Posted on 2009-05-15
3
508 Views
Last Modified: 2012-05-07
I'm in the process of building a set of custom classes to wrap up a custom OLEDB provider, so I'm recreating classes such as MyDbConnection, MyDbCommand etc.  The problem is that when using an interface, such as IDbConnection, it returns an interface type.

For example, I have a MyDbCommand class (using IDbCommand) and a MyDbConnection class (using IDbConnection).  When I provide an method for the MyDbConnection.CreateCommand() it returns a IDbCommand, not a MyDbCommand as I want it to.

When I look at the 'standard' classes such as OleDbConnection, it's CreateCommand() method returns an OleDbCommand object, not a IDbCommand object so I'm guessing this is possible?

The method in the MyDbConnection as the moment looks like:

public IDbCommand CreateCommand()
{
    return new MyDbCommand();
}

which compiles, but then requires conversion when used in another project.  Am I missing a keyword/method in order to do this?
0
Comment
Question by:d_brentley
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
we are using something what you are trying to do but we are using the enterprise library to achieve the functionality

we achieve something like this
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
protected Database Create (string databaseInstance)
{
      m_dbProvider = ConfigurationManager.ConnectionStrings[databaseInstance].ProviderName;
      switch (m_dbProvider)
      {
            case DbProviderMapping.DefaultOracleProviderName:
            return new OracleDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            case DbProviderMapping.DefaultSqlProviderName:
            return new SqlDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            case DbProviderMapping.DefaultMySqlProviderName:
            return new MySqlDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            default:
            return DatabaseFactory.CreateDatabase (databaseInstance);
      }
}
0
 
LVL 9

Accepted Solution

by:
tculler earned 125 total points
Comment Utility
Interfaces are set-in-stone, and they are that way or a very good reason. If you changed the return type on a global interface, yet then used it at a later time, you'd get that other return type; one that you may not want.

Instead, I would make a seperate method--one to satisfy the interface, and one to avoid the "casting" syntax. It is a good idea to still implement the interface, though.
public IDbCommand CreateCommand()

{

     // Shut the interface up, return your command

}
 

public MyDbCommand CreateMyCommand()

{

     // Return your command

}

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now