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

匆匆过客 提交于 2019-12-10 10:56:27

问题


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?


回答1:


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



回答2:


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




回答3:


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




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/450463/how-to-make-this-class-generic-net-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!