Handle IDisposable objects When I work a lot with objects, that implements IDisposable I am using this pattern, like shown here with a SqlConnection and a SqlCommand.

    private string _connectionString = "";

    public IEnumerable&lt;User&gt; GetUsers()
    {
        List&lt;User&gt; users = new List&lt;User&gt;();
        string sql = "SELECT * FROM [user]";
        InvokeSql(sql, (connection, command) =&gt;
        {
            IDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                User user = new User();
                users.Add(user);

                user.Name = reader["name"] as string;
            }
        });
        return users;
    }

    private void InvokeSql(string sql, Action&lt;SqlConnection, SqlCommand&gt; action)
    {
        if (string.IsNullOrWhiteSpace(sql))
        {
            throw new ArgumentNullException("sql");
        }

        if (action == null)
        {
            throw new ArgumentNullException("action");
        }

        using (SqlConnection connection = new SqlConnection(_connectionString))
        using (SqlCommand command = new SqlCommand(sql, connection))
        {
            connection.Open();
            action(connection, command);
        }
    }


So I can reuse the InvokeSql and do not have to bother about the disposing.
But is it really a good idea, to use an Action in a using Statement?
So I can reuse the InvokeSql and do not have to bother about the disposing.


Sure. Except InvokeSql is private, so reuse is rather limited to that class. And if that class has a GetUsers method, I dare expect only User-related stuff in that class... which InvokeSql isn't.

In all likelihood, your model is going to involve more than just User entities; I'd make a base class to encapsulate the CRUD and this InvokeSql method; then a derived type can be responsible for User entities, and another derived type can be responsible for another entity type - something like a "repository".

ExecuteReader returns a SqlDataReader instance, which also implements IDisposable: you're missing a using block here:


IDataReader reader = command.ExecuteReader();
while (reader.Read())



using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    ...