Jader Dias
Jader Dias

Reputation: 90465

How to make this class generic? (.NET C#)

My class has the following core:

class SmartDbConnection
{
    private readonly IDbConnection Connection;

    public SmartDbConnection(string ConnectionString)
    {
        if(ConnectionString.Contains("MultipleActiveResultSets=true"))
        {
            Connection = new SqlConnection(ConnectionString);
        }
    }
}

I don't want it to have "SqlConnection" hardcoded. So I thought in making it a Generic class (accepting IDbConnection classes). But I don't know how to do it. Anyone can help?

Upvotes: 2

Views: 8534

Answers (5)

Serge Wautier
Serge Wautier

Reputation: 21878

  class SmartDbConnection<T> where T: IDbConnection , new()
  {
    private readonly T Connection;

    public SmartDbConnection(string ConnectionString)
    {
      if (ConnectionString.Contains("MultipleActiveResultSets=true"))
      {
        Connection = new T();
        Connection.ConnectionString = ConnectionString;
      }
    }
  }

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062600

First - I've added IDisposable to this, as I believe it is important.

Second, note that providers are an alternative here:

class SmartDbConnection
{
    private DbConnection Connection;

    public SmartDbConnection(string provider, string connectionString)
    {
        Connection = DbProviderFactories.GetFactory(provider)
            .CreateConnection();
        Connection.ConnectionString = connectionString;
    }
    public void Dispose() {
        if (Connection != null)
        {
            Connection.Dispose();
            Connection = null;
        }
    }
}

If you must go generic, how about:

class SmartDbConnection<T> : IDisposable where T : class,
    IDbConnection, new()
{
    private T Connection;

    public SmartDbConnection(string connectionString)
    {
        T t = new T();
        t.ConnectionString = connectionString;
        // etc
    }
    public void Dispose() {
        if (Connection != null)
        {
            Connection.Dispose();
            Connection = null;
        }
    }
}

Upvotes: 10

Jon Skeet
Jon Skeet

Reputation: 1500055

If you don't want to specify SqlConnection there, where would you specify it - and how would you know to use it only if the connection string contains "MultipleActiveResultSets=true"?

I suspect at some level you want a connection factory - either a Func<string, IDbConnection> you can pass in or set somewhere, or possibly just a class:

public static class ConnectionFactory
{
    public static IDbConnection CreateConnection(string connectionString)
    {
        // Hard-code stuff here
    }
}

Of course, they're just two sides of the same coin - ConnectionFactory is just a static implementation of the Func<string, IDbConnection>.

Upvotes: 1

Fabrizio C.
Fabrizio C.

Reputation: 1564

Maybe...

class SmartDbConnection<T> where T : IDbConnection, new()
{
    private readonly IDbConnection Connection;

    public SmartDbConnection(string connectionString)
    {
        if (connectionString.Contains("MultipleActiveResultSets=true"))
        {
            Connection = new T();
            Connection.ConnectionString = connectionString;
        }
    }
}

EDIT: But what kaanbardak suggests can be even better...

Upvotes: 2

user53378
user53378

Reputation: 160

Why don't you accept IDbConnection instead of connectionstring to your ctor?

Upvotes: 8

Related Questions