简体   繁体   English

如何使这个类通用? (.NET C#)

[英]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. 我不希望它有“SqlConnection”硬编码。 So I thought in making it a Generic class (accepting IDbConnection classes). 所以我想把它变成一个Generic类(接受IDbConnection类)。 But I don't know how to do it. 但我不知道该怎么做。 Anyone can help? 有人可以帮忙吗?

First - I've added IDisposable to this, as I believe it is important. 首先 - 我已经添加了IDisposable ,因为我认为这很重要。

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

为什么不接受IDbConnection而不是连接到你的ctor?

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... 编辑:但是kaanbardak建议可以更好......

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"? 如果你不想在那里指定SqlConnection,你会在哪里指定它 - 只有当连接字符串包含“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: 我怀疑在某种程度上你想要一个连接工厂 - 你可以传入或设置一个Func<string, IDbConnection> ,或者可能只是一个类:

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> . 当然,它们只是同一枚硬币的两面 - ConnectionFactory只是Func<string, IDbConnection>的静态实现。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM