I have a Data Access Layer library that I would like to make "portable". The reason I like it to be portable is because I want to work with SQL Azure & Azure File Storage (eg, data + pdf reports) as well as Sql Server 2008R2 and File System storage on a concrete server.
Based on spec the system is supposed to go live with the later implementation (sql + file system storage), while upon a meeting a certain scalability threshold we plan on moving to Azure.
The data access class I use implements IDataProvider interface (which I built) and it defines the methods that any data access concrete implementation should have. Consuming the data access layer is done via passing the IDataProvider interface and calling methods on it eg:
public Interface IDataProvider
{
public bool DoSomething();
}
public class AzureDataProvider : IDataProvider
{
private string ConnectionString;
public AzureDataProvider(string connectionString)
{
this.ConnectionString = connectionString;
}
public AzureDataProvider():this(
ConfigurationManager.ConnectionString["conn"].ConnectionString)
{
}
public bool DoSomething()
{
return false;
}
}
So now the issue is that the consumer class that would call methods on the IDataProvider interface has to do the following:
public class DataAccessConsumer
{
public void SomeOperation()
{
AzureDataProvider azureProvider = new AzureDataProvider();
IDataProvider dataProvider = (IDataProvider)azureProvider;
bool result = dataProvider.DoSomething();
}
}
So the issue with the above code is that the client still has to have knowledge of the concrete AzureDataProvider class. I want to have a method of providing the client with just the IDataProvider interface, without passing a connection string to every interface method, or looking up the connection string within every method via the ConfigurationManager.
Is that possible? Would Abstract Factory or some sort of Dependency Injection Container pattern do the trick? If so, I would appreciate code samples, or links to code samples.