问题
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:
- How the projects are structured
- Any best practices that need to be followed
- How Domain objects/POCOs, DTOs, ViewModels need to be organized, and communicate between each other
- Unit Testing approaches
All your suggestions/advices on these areas are highly appreciated
Thank you very much.
回答1:
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.
来源:https://stackoverflow.com/questions/7310652/wcf-with-asp-net-mvc-reference-projects