Search code examples
c#resharpersqlconnectionsanitization

Can the DbProviderFactory.CreateCommandBuilder actually return a null?


I use the following pattern to sanitize database names:

var dbProviderFactory = DbProviderFactories.GetFactory(connection);
using (var commandBuilder = dbProviderFactory.CreateCommandBuilder())
{
    var fooSafe = commandBuilder.QuoteIdentifier(foo)
}

but each time I do this R# complains about the commandBuilder and says

Possible System.NullReference exception.

To get rid of this warning I usually put

Debug.Assert(commandBuilder != null);

after creating the command builder.

I use mostly a SqlConnection or SQLiteConnection.

There is not much about it in the documentation that just says:

Returns a new instance of the provider's class that implements the DbCommandBuilder class.

I'm never sure whether I should check the command builder for null or just ignore the warning?


Solution

  • since the documentation says it will return a "new instance" i wouldn't worry about null check.

    Resharper is complaining possibly because it is a virtual method and any custom implementation may return null.