Casting question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Blair

    #1

    Casting question

    Hi,

    I have the follwoing code:

    ( (cSqlProviderFa ctory)m_factory ).GetConnection String();
    ( (cODBCProviderF actory)m_factor y ).GetConnection String();

    I want this to be one line only (I have quite few different classes)

    Is it possible, in C# to do soemthing like this:

    Type myType = m_Factory.GetTy pe();
    ( (myType)m_facto ry ).GetConnection String();

    Regards,

    Steven



    *** Sent via Developersdex http://www.developersdex.com ***
  • Richard Blewett [DevelopMentor]

    #2
    Re: Casting question

    Going blind here as you don't say what the relationship between the factory classes is

    abstract class ProviderFactory
    {
    public abstract string GetConnectionSt ring();
    }

    class cSqlProviderFac tory : ProviderFactory
    {
    public override string GetConnectionSt ring();
    {
    return .....;
    }
    }

    class

    class cODBCProviderFa ctory : ProviderFactory
    {
    public override string GetConnectionSt ring();
    {
    return .....;
    }
    }

    class Foo
    {
    ProviderFactory m_factory;
    public void UseFactory()
    {
    string connStr = m_factory.GetCo nnectionString( );
    }
    }

    This uses polymorphism to get the appropriate connection string only knowing about the base factory

    Whether this does what you want depends on your implementation however, if this doesn't meet you requirements you'll have to give us more information.

    Regards

    Richard Blewett - DevelopMentor



    Hi,

    I have the follwoing code:

    ( (cSqlProviderFa ctory)m_factory ).GetConnection String();
    ( (cODBCProviderF actory)m_factor y ).GetConnection String();

    I want this to be one line only (I have quite few different classes)

    Is it possible, in C# to do soemthing like this:

    Type myType = m_Factory.GetTy pe();
    ( (myType)m_facto ry ).GetConnection String();

    Regards,

    Steven

    Comment

    • Steven Blair

      #3
      Re: Casting question

      I have defined an interface which all my DB classes inherit from:

      public interface IDbProviderFact ory
      {
      IDbConnection CreateConnectio n( string connectionStrin g );
      }


      And a class inherits from this:

      public class cSqlProviderFac tory : IDbProviderFact ory
      {
      public IDbConnection CreateConnectio n( string c )
      {
      return new SqlConnection( connectionStrin g );
      }

      I want each inheritted class to have its own GetConnectionSt ring, but
      each version has the possibility of different parameters being passed
      in.

      Hope this is more helpful.

      Regards,

      Steven




      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Richard Blewett [DevelopMentor]

        #4
        Re: Casting question

        Well I don't know if this was intentional but your design is almost how ADO.NET 2.0 is architected for the next release of .NET. Here they define a ConnectionStrin gFactory class that has all the standard things like server, database, etc and also a hashtable to put the other key/value pairs. You can override ToSTring in your version to return the complete provider specific connection string. So GetConnectionSt ring returns the factory which is used to create the bits of teh connection string.

        Regards

        Richard Blewett - DevelopMentor



        I have defined an interface which all my DB classes inherit from:

        public interface IDbProviderFact ory
        {
        IDbConnection CreateConnectio n( string connectionStrin g );
        }


        And a class inherits from this:

        public class cSqlProviderFac tory : IDbProviderFact ory
        {
        public IDbConnection CreateConnectio n( string c )
        {
        return new SqlConnection( connectionStrin g );
        }

        I want each inheritted class to have its own GetConnectionSt ring, but
        each version has the possibility of different parameters being passed
        in.

        Hope this is more helpful.

        Regards,

        Steven

        Comment

        • Chris Dunaway

          #5
          Re: Casting question

          Then you should be casting to the Interface rather than to the class
          name:

          ( (IDbProviderFac tory)m_factory ).GetConnection String();



          And, by the way, your classes do not inherit from the interface, they
          implement it.

          Cheers

          Comment

          Working...