I have some legacy code using workflow foundation that has some performance issues , I'm seeing this pattern repeated . does the db.CreateCommand()
have any preformace hit , is there any other way to create a DbCommand
object ?
DbCommand CreateCommand(DbConnection db)
{
using (var cmd = db.CreateCommand())
{
...
return cmd;
}
}
is there any other way to create a DbCommand object ?
Not in a provider-agnostic way. Since you are using the abstract DbConnection
1 class instead of a specific provider, you need to use the factory method to ensure that the right connection type is used.
Note that in general connections are pooled by .NET, so creating then generally isn't an expensive process. If you want to know in you actual situation if you have a performance problem then try it both ways and measure the difference, otherwise you (and we) are just speculating.
I would also reiterate what is mentioned in the comments, that you are returning a disposed object. It's not clear what you do with the command other than just creating it, but returning a disposed object is probably going to result in the client trying to use an object in a bad state and getting errors. Best to let the client dispose of it rather than this method.