How to Switch Dynamically between XML and SQL Repository in Web Service/ Web API?

喜欢而已 提交于 2019-12-24 10:38:18

问题


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."


回答1:


Go to the mvc project and add nuget package Unity.Mvc3 or mvc4 based on your project. This will bring a class called Bootstrapper.cs in the project. Add your type here as shown:

 public static class Bootstrapper
{
    public static void Initialise()
    {
        var container = BuildUnityContainer();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        //Register the repository
        container.RegisterType<IProductRepository, SQLProductRepository>();
        return container;
    }
}

In the global.config called the bootstrapper.initialise():

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
        Bootstrapper.Initialise();

    }

You can now set up controllers like this:

 public class HomeController : Controller
{
    private readonly IProductRepository productRepository;

    public HomeController(IProductRepository productRepository)
    {
        this.productRepository = productRepository;
    }

and the correct type will be sent. You can change the implementation any time in bootstrapper.cs without changing any code.




回答2:


If you can distinguish which repository is to be used based on the HTTP verb being submitted, you can configure your routes based on that. More details about that can be found in this question.



来源:https://stackoverflow.com/questions/24272050/how-to-switch-dynamically-between-xml-and-sql-repository-in-web-service-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!