I need to be able to support clients on both MySQL and PostGres databases in a legacy application that does not use any ORM. Say, I have the following interface to query a list of users.
public interface IUsers
{
...
}
I have two implementations for the above interface. One for MySQL
public class MySQLUsers : IUsers
{
...
}
and one for PostGres
public class PostGresUsers: IUsers
{
...
}
How can I get the right implementation at runtime? Ideally, I'd like my business layer to be able to call the methods on the interface without having any knowledge of whether the data was coming from MySQL or PostGres. I am able to achieve this currently using the following code but it can quickly get unmaintainable if I have to do this for every single interface that has database code in it.
IUsers GetUsersProvider()
{
if(something)
{
return new MySQLUsers();
}
else
{
return new PostGresUsers();
}
}
Are there any recommended patterns I can use to do this?