Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use a separate web application for hosting the web services. This will give you the possibility to host your MVC application and WCF service in separate virtual directories in IIS. Once you wrote the web service you could generate a client proxy and then in the client application you could use a repository:</p> <pre><code>public interface IProductsRepository { IEnumerable&lt;Person&gt; GetProducts(); } </code></pre> <p>and then have a specific implementation of this repository that will fetch the data from your WCF service:</p> <pre><code>public class ProductsRepositoryWcf { public IEnumerable&lt;Person&gt; GetProducts() { using (var client = new YourWebServiceClient()) { // call the web service method return client.GetProducts(); } } } </code></pre> <p>And finally inject this repository into the constructor of your controller which might look like this:</p> <pre><code>public class HomeController: Controller { private readonly IProductsRepository _repository; public HomeController(IProductsRepository repository) { _repository = repository; } public ActionResult Index() { var products = _repository.GetProducts(); // TODO: An intermediary step might be necessary to convert the Product // model coming from the web service to a view model which is adapted // to the given view return View(products); } } </code></pre> <p>As you can see now the controller is completely decoupled by the way data is fetched. All it cares is that it respects the given contract (IProductsRepository interface). Using your favorite DI framework you could easily switch the implementation. </p> <p>By the way if your code resembles mine, theonly thing that you should change in your current MVC application is to externalize the models and data access layers into a separate WCF service project which you would add service reference to, implement the <code>ProductsRepositoryWcf</code> repository and instruct your DI framework to use this implementation instead of the <code>ProductsRepositorySql</code> which would now go to the web service.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload