简体   繁体   中英

If I have concern over the reliability of OleDbParameterCollection.Clear, what are my options to validate the method works?

In my code I make several database operations using the same OleDbCommand (I am using MS Access). Since I'm reusing the same command object I have to call Clear() on it every time before each reuse. If I have concern over the reliability of calling Clear() , what are my options to validate the method works as described by the API?

Your question seems to be "Does the OleDbParameterCollection.Clear " method do what the API says it does ("Removes all OleDbParameter objects from the OleDbParameterCollection.")?

Option 1: Trust the API.

Generally you should not doubt API's you're given (if you're not responsible for maintaining them). If you're really paranoid, choose a different option (not generally recommended).

Option 2: Manually test the API.

Create a sample Console Application project that tests the API in question to make sure it behaves as you're expecting. This will let you see precisely how it works, particularly if your calling code is hard automate or debug.

Option 3: Unit test the API.

Create an automated that tests the API in question and automate it to run it after each automated build. If you substitute out the API for a different version you'll notice a difference immediately.

Option 4: Error if the API fails.

If you're worried about how an API works you could do some sanity checks and throw an exception (or whatever behavior you want at runtime) if the API fails to behave as you're expecting. If you call the API in multiple places it might be good to use something like an extension method or decorator pattern so that each call to the API gets the sanity checks. Here's an example of how to do that with an extension method (untested):

public static class OleDbParameterCollectionExtensions
{
    // <summary>
    // Calls Clear() on the OleDbParameterCollection and performs
    // a check to make sure it succeeds.  If it does not succeed
    // an exception will be thrown.
    // </summary>
    public static void SafeClear(this OleDbParameterCollection @this)
    {
        // Call the method.
        @this.Clear();

        // Do the sanity check
        if (@this.Count != 0)
        {
            throw new Exception(@"OleDbParameterCollection.Clear() failed.");
        }
    }
}

// Usage:
OleDbParameterCollection collection = /* ... */;
collection.SafeClear();

Option 5: Make a new OleDbCommand each time you need one.

Unless you need to reuse it for performance reasons (that you can measure), I think it's intended to be single-use. It implements IDisposable , so using it inside a using will safely clean it up for you. Here's a sample usage (untested):

string connectionString = "...";
using (var connection = new OleDbConnection(connectionString))
{
    connection.Open();

    string query = "...";
    using (var command = new OleDbCommand(query, connection))
    {
        /* Initialize command.Parameters */
        command.ExecuteNonQuery();
    }
}

No matter which option you choose, your validation process (QA, automated tests, etc.) should detect the API not working as expecting, anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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