问题
I have this project where I try to implement a methodology so i can connect to any database. Following this site (and google), the best practice to achieve that is with a factory pattern (I've seen a few people referring to an abstract factory pattern but i never used it so i stayed with a factory pattern). So I've approached this problem with a factory pattern and created a interface for my Connection which looks like this:
interface IConnection
{
string ConnectionString { get; set;}
IDbConnection CreateConnection { get; }
}
I'm using IDbConnection
since the main db providers seems to implement it.
I'm not sure if I need anything else there.
After this I created my Factory class which look like this:
class clsFactoryDB
{
static public IConnection CreateDBConnection(int cChoice)
{
IConnection ObjSelector = null;
switch (cChoice)
{
case 1:
ObjSelector = new clsSQL();
break;
default:
ObjSelector = new clsSQL();
break;
}
return ObjSelector;
}
}
For now I just defined SQL but MySQL or Oracle follow the same procedure so I don't bother I'll add it later.
And finally I created my IConnection
implementation like that:
class clsSQL : IConnection
{
private DbConnection connection;
string IConnection.ConnectionString
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
IDbConnection IConnection.CreateConnection
{
get
{
if (connection == null)
{
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnection"].ConnectionString);
}
return connection;
}
}
}
Now in my app.config I've added a property for all the connection strings which I'm referring with ConfigurationManager
.
<connectionStrings>
<add name ="SqlServerConnection" connectionString="SQL..." providerName="System.Data.SqlClient"/>
<add name ="MySqlServerConnection" connectionString="MySQL..." providerName="MySql.Data.MySqlClient"/>
//add more
</connectionStrings>
So my first question is: Is my implementation of this concept right or am i missing something? Again to make this I only followed some topics already posted here, I've also read bit on code project but their stuff is always confusing.
Now my confusion comes from the fact that to make this work the connection string and provider has to be declared in app.config file (or web.config for asp). But I'm not always connecting to the same server, so I need to change the connection string (that's why i have added a ConnectionString
method in my IConnection
interface). Am I right? Therefore if I understand this correctly, I now can manipulate any db with ONE object just by changing the connection string as needed to connect to different databases.
Please correct any mistakes I could have made here. Thank you.
来源:https://stackoverflow.com/questions/44283370/any-db-connection-with-factory-pattern-need-clarification-and-review-of-my-meth