Using Multiple Datasources

J

jeff

I have an application that can either connect to an access database or
a SQL Server database. I have 2 assemblies both implement the same
interface for describing the data access methods for the application.
One assembly is specific to use the Access database using the OLEDB
provider, the other is specific to SQL Server using the SQLServer
provider. I want to be able to use the SQL server provider for the SQL
server assembly.
My original thought was to have 2 compiled versions of the app, one for
Access and one for SQL Server. I believe there is a better way to do
this using the app.config file but am not sure exactly how to go about
it.

Any thoughts?

Thanks in advance

Jeff D.
 
S

sloan

If you have the same interface, then you've done most of the work.

Go to:
12/1/2005
Understanding the Simple Factory Pattern

http://sholliday.spaces.live.com/blog/


I would use "reflection", or the "key" method. And create a Factory class.
( A factory class is a class that creates other classes)


You could have the key value in your config file.

Then you'd need a factory

public class TalkToDatabaseFactory
{

public static IMyDatabaseAccessInterface GetMyDatabaseAccessInterface ()
{
string key = "Access"; // read this from web.config file

switch (key.ToUpper())
{
case "ACCESS":
return new MyDatabaseAccess();
case "SQLSERVER":
return new SqlServerMyDatabaseAccess();

default:
throw new ArgumentException("The AnimalFactory was given a Bad Key");

}
}
}
 
R

Registered User

I have an application that can either connect to an access database or
a SQL Server database. I have 2 assemblies both implement the same
interface for describing the data access methods for the application.
One assembly is specific to use the Access database using the OLEDB
provider, the other is specific to SQL Server using the SQLServer
provider. I want to be able to use the SQL server provider for the SQL
server assembly.
My original thought was to have 2 compiled versions of the app, one for
Access and one for SQL Server. I believe there is a better way to do
this using the app.config file but am not sure exactly how to go about
it.

Any thoughts?
Two data sources and a common interface sounds like you might want to
use the provider model. In the app.config one of the providers can be
set as the default depending upon the desired data source.

regards
A.G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top