Is there a factory way in ADO.NET to get a parameter flag?

I use the ADO.NET interfaces to create a database independent program. factory takes the name of the provider and returns specific vendor objects that implement the ADO interface. It's great.

But I can not find a factory for the flag that identifies the parameter.

string paramName = "@foo"; //flag "@" for sql server string paramName = ":foo"; //flag ":" for oracle 

Question: Does ADO.NET provide a factory method for receiving a flag?
I could collapse my own separate factory for this, but that would limit me to a predefined list of suppliers; defeating most of the benefits of a factory provider.

+4
source share
1 answer

Unfortunately, the DbProviderFactory mechanism is very poorly designed by IMHO. The mechanism itself is not expandable, all interesting classes are sealed (SqlClientFactory, etc.), the configuration in the .config file is also not extensible.

One simple solution is to build an extension class, for example:

 public static class DbProviderFactoryExtensions { public static DbParameter CreateParameter(this DbProviderFactory factory, string name) { DbParameter parameter = factory.CreateParameter(); if (factory.GetType().FullName.IndexOf("sqlclient", StringComparison.OrdinalIgnoreCase) >= 0) { name = "@" + name; } else if (factory.GetType().FullName.IndexOf("oracle", StringComparison.OrdinalIgnoreCase) >= 0) { name = ":" + name; } // etc... parameter.ParameterName = name; return parameter; } } 

This is not very smart, but you can do it in your code:

  DbProviderFactory factory = DbProviderFactories.GetFactory("MyDb"); DbParameter parameter = factory.CreateParameter("myParam"); // parameter.ParameterName will be @myParam if SQL, etc. 

Or you can also recreate your own system. This is not very complicated and may be worth it if you need other variables for each type of database.

+2
source

All Articles