Ryan D.
Ryan D.

Reputation: 57

How to pass an object and access methods?

I am trying to issue commands to a database but I want to have the ability to connect to different ones (eg. SQL Express, PostgreSQL, SQLite)

I have a setting that tells the application which database is being used, but the syntax for the SQL code is slightly different so I have a class for Postgres and a class for SQLite. What I want to do is define the same methods in different classes and pass the class. I've written this:

public static class DatabaseConnection
{
    public static string PostgreSQLConnectionString { get; set; }
    public static string SQLiteConnectionString { get; set; }

    public static object Interface(string connectiontype)
    {
        switch (connectiontype)
        {
            case "PostgreSQL":
                PostgresConnection postgresConnection = new PostgresConnection();
                return postgresConnection;
            case "SQLite":
                SQLiteConnection sqliteConnection = new SQLiteConnection();
                return sqliteConnection;
        }
        return null;
    }
}

Which I then want to reference like:

Object Connection = DatabaseConnection.Interface("PostgreSQL");
Connection.Connect()

Where the method Connect() exists in all classes. This of course doesn't work since the class isn't directly named so I can't call methods that may not exist. I'm currently getting around this by copying all the methods into the DatabaseConnection class and using switch statements in every method. Is there a better way to accomplish the same effect?

Upvotes: 0

Views: 102

Answers (2)

Grzesiek Danowski
Grzesiek Danowski

Reputation: 467

I suggest to create IRepository interface that will define all database operation and then different implementation for different databases or few interfaces for example IUserRepository, IWorkRepository etc. Polymorphism is better then use switch - your db client can rely on interface and you can add additional implementations using another databases.

Upvotes: 1

Neil
Neil

Reputation: 11919

You are looking for the Factory pattern to create the instance, and an interface to implement the connection:

public interface IConnection
{
    void Connect();
}
public class PostgresConnection : IConnection
{
    public void Connect()
    {
    ...
    }
}
public class SqliteConnection : IConnection
{
    public void Connect()
    {
    ...
    }
}

public static class DatabaseConnectionFactory
{
    public static string PostgreSQLConnectionString { get; set; }
    public static string SQLiteConnectionString { get; set; }

    public static IConnection Create(string connectiontype)
    {
        switch (connectiontype)
        {
            case "PostgreSQL":
                PostgresConnection postgresConnection = new PostgresConnection();
                return postgresConnection;
            case "SQLite":
                SQLiteConnection sqliteConnection = new SQLiteConnection();
                 return sqliteConnection;
        }
        return null;
    }
}

But, I would recommend you look at something like EntityFramework or Dapper, both of which will be much easier to write for as they support many/all the database types you could ever hope to use.

Upvotes: 2

Related Questions