Untitled

 avatar
unknown
plain_text
5 months ago
1.3 kB
1
Indexable
public interface IDbConnectionFactory
{
    IDbConnection CreateConnection(string connectionString);
}

public interface IDbConnection : IDisposable
{
    void Open();
    void Close();
    IDbCommand CreateCommand(string commandText);
}

public interface IDbCommand
{
    int ExecuteNonQuery();
}

// Concrete Implementations
public class SqlDbConnectionFactory : IDbConnectionFactory
{
    public IDbConnection CreateConnection(string connectionString)
    {
        return new SqlDbConnectionWrapper(new SqlConnection(connectionString));
    }
}

public class SqlDbConnectionWrapper : IDbConnection
{
    private readonly SqlConnection _sqlConnection;

    public SqlDbConnectionWrapper(SqlConnection sqlConnection)
    {
        _sqlConnection = sqlConnection;
    }

    public void Open() => _sqlConnection.Open();

    public void Close() => _sqlConnection.Close();

    public IDbCommand CreateCommand(string commandText)
    {
        return new SqlDbCommandWrapper(new SqlCommand(commandText, _sqlConnection));
    }

    public void Dispose() => _sqlConnection.Dispose();
}

public class SqlDbCommandWrapper : IDbCommand
{
    private readonly SqlCommand _sqlCommand;

    public SqlDbCommandWrapper(SqlCommand sqlCommand)
    {
        _sqlCommand = sqlCommand;
    }

    public int ExecuteNonQuery() => _sqlCommand.ExecuteNonQuery();
}
Editor is loading...
Leave a Comment