Eranga Dissanayaka
Eranga Dissanayaka

Reputation: 1930

WCF with ASP.NET MVC - Reference Projects

on a ASP.NET MVC project, the data access layer is going to be implemented in WCF. Reason is that these WCF services are going to be consumed by couple of other client applications in the future.

Can I know whether there are any good reference projects that I can have a look at. Important things that I need to have a look at are:


All your suggestions/advices on these areas are highly appreciated


Thank you very much.

Upvotes: 1

Views: 455

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You would do exactly the same as if you were doing any other ASP.NET MVC application. You would simply provide an implementation of your repository which would call the WCF service. So basically your repository interface could be the operation contract you got when you imported your service:

public interface IProductsService
{
    IEnumerable<Product> GetProducts();
}

Product and IProductsService are domain objects coming from your WCF service. In the implementation of this interface you would delegate the call to the service itself. As far as the rest of the application is concerned, your controllers doesn't really care as they are weakly coupled:

public class ProductsController: Controller
{
    private readonly IProductsService _productsService;
    public ProductsController(IProductsService productsService)
    {
        _productsService = productsService;
    }

    public ActionResult Index()
    {
        var products = _productsService.GetProducts();
        var productsVm = Mapper.Map<IEnumerable<Product>, IEnumerable<ProductViewModel>>(products);
        return View(productsVm);
    }
}

Pretty standard stuff and it is how your controllers should look like no matter where the data comes from. As you can see if you always design your applications with abstractions you could very easily switch implementation of this IProductsService that will be injected into your controllers and from the ASP.NET MVC application standpoint it wouldn't even make any difference. The view models should be part of the web UI as they are strongly tied to the views.

The service contracts and domain models go into a service layer.

Upvotes: 2

Related Questions