I have Web Service to carry CRUD operation. I have IProductRepository interface implemented by XMLProductRepository and SQLProductRepository.
Now i use the repository instance in my controller of the web service to invoke Get/Put/POST and Delete operations which are defined respectively in XMLRepository and SQLRepository. But i am getting little confused as how to change dynamically between these two repositories as i want to make my web service database agnostic
public interface IProductRepository
{
IEnumerable<Product> GetAll();
Product Get(int id);
Product Add(Product item);
void Remove(int id);
bool Update(Product item);
}
public class XMLProductRepository : IProductRepository
{
public XMLProductRepository() {}
public IEnumerable<Product> GetAll() {}
public Product Get(int id) {}
public Product Add(Product item) {}
public void Remove(int id) {}
public bool Update(Product item) {}
}
public class SQLProductRepository : IProductRepository
{
public SQLProductRepository() {}
public IEnumerable<Product> GetAll() {}
public Product Get(int id) {}
public Product Add(Product item) {}
public void Remove(int id) {}
public bool Update(Product item) {}
}
public class ProductController : ApiController
{
static readonly IProductRepository repository = new XMLProductRepository();
// REST OF THE CODE AND IMPLEMENTATION HERE...
// using the repository object
}
The question is how do i make it dynamic here, instead of defining the specific repository object in controller? Or the question is, can i even do that for this WEB API?
Let me add further details to the Question - Thanks to Dillie-O for pointing it out.
The requirement i have got is "Sources are mutually exclusive. At any point of time, service picks information from only one source either XML or SQL. The Service should be able to switch between sources without client knowledge. In addition to that Service WEB API shouldn't change whenever my source changes."