When I instantiate a SqlConnection object in ADO.Net, then does this code result in the execution of corresponding DbProviderFactory code ? So when the code in block 2 is executed by, then we actually end up executing code in block 1. But I am not sure if this is true.
CODE BLOCK 1 – Instantiate a SqlConnection using DbProviderFactory approach
DbProviderFactory factory =
DbProviderFactories.GetFactory(providerName);
connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
CODE BLOCK 2 -Instantiate a SqlConnection using standard ADO.Net code
SqlConnection con = new SqlConnection(connectionString);
I had a look at some .net Framework assemblies and found the following
DbProviderFactories.GetFactory(providerName)will return an Factory object according to the given providerName.Let’s assume providerName indicates an SQL Provider so we will get an
SqlClientFactory.Afterwards
factory.CreateConnection()will be called. In this caseSqlClientFactory.CreateConnection()will be called which is implemented asI think the answer to your question is that calling the factory methods will call the methods of the provider specific classes and not the other way round!