How to make an application Database independant

  • Thread starter Gustavo De la Espriella
  • Start date
G

Gustavo De la Espriella

Hi,

What's a good way to make an application automatically choose between
SQLClient, ODBC, Oracle and OleDB depending on user's choice?
Thanks,
Gustavo De la Espriella
 
B

BG

Maybe I'm not understanding the question, however, how about having multiple
connection strings and using one of them based on what the user selected
from a radiobuttonlist or dropdownlist, etc. ?
 
N

Newbie

Yeah, me too, though I'm not too understand the question here's my opinion.

I don't think it's a good idea to have several kinds of backend running
altogether for an applicaiton. So, I'll assume that you only have one active
backend for the appl.
IMHO, It will depends much on what backend you use for that appl.
i.e: SqlClient is optimized when you use Sql Server for the backend, Oracle
is for Oracle DB and so on.

rgds,
andy
 
G

Gustavo De la Espriella

Its for an comercial application that needs to use whatever database the
client prefers a main database, even after the initial installation. I'll
try to understand the Web links from Karl to see if it's what we need.

Thanks,
Gustavo De la Espriella
 
M

Matt Berther

Hello Karl,

While the Provider model is a viable solution for the problem here, I believe
that a better solution might be much simpler by using the AbstractFactory
pattern [1] in conjunction with the DataMapper pattern [2].

For example:

// This class doesnt necessarily need to exist. We could create DataFactory
as an abstract class and define GetDataFactory as a static method on that
class. The choice is yours.
public class DataManager
{
public IDataFactory GetDataFactory()
{
// some code to read the configuration which will determine which
concrete implementation of IDataFactory to return
string dataFactoryType = ConfigurationSettings.AppSettings["dataFactoryType"];
Type type = Type.GetType(dataFactoryType);
return (IDataFactory)Activator.CreateInstance(type); // of course,
you'll probably want to do some caching of this
// since reflection can be expensive. Borrowing a page from the Provider
design pattern implementation, we could cache
// the ConstructorInfo object and invoke it.
// Another option would be to expose this DataManager as a singleton.
}
}

public interface DataFactory
{
IUserMapper GetUserMapper();
}

public interface IUserMapper
{
User Get(string id);
Insert(User user);
Update(User user);
Delete(User user);
}

// Data store provider implementation
class SqlDataFactory : IDataFactory
{
public IUserMapper GetUserMapper()
{
return new SqlUserMapper();
}
}

class SqlUserMapper : IUserMapper
{
// IUserMapper implementation goes here.
}

This is a great pattern to implement, because it easily allows for future
growth. Since all data access is done through the IDataFactory interface
as well as the DataMapper interfaces, the client has no concept of the underlying
data store.

The only things I have to do to add say Oracle support is implement an IDataFactory
that returns the DataMapper classes for the Oracle database.

[1] http://c2.com/cgi/wiki?AbstractFactory
[2] http://www.martinfowler.com/eaaCatalog/dataMapper.html
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top