Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct architecture for MVC4 WebAPI

I ran some searches but found no available answer to this issue.

Okay, my MVC 3 architecture is like this:

- Project.EDM (contains only the entity framework edmx file and its .tt and .cs entity classes)
- Project.DAL (contains IXxxRepositiory and XxxRepository where CRUD is performed on the entity classes)
- Project.Web (the MVC3 project. Controllers transfer data from ViewModels into entity models and call the CRUD functions in the DAL repositories.)

The WebApi in MVC4 appears so attractive since we will be able to call the CRUD operations from other applications. We all love this idea.

But the existing examples I have found have the CRUD operations inside the MVC4 project under ApiController. I am used to putting these operations into a separate DAL project. What is the recommended choice? Can we still have a separate DAL class? How do you experts design the architecture?

Thank you for all helpful advice.

like image 928
Blaise Avatar asked May 18 '12 13:05

Blaise


1 Answers

What I do is this:

  • Repository to query the database
    • Service layer to validate stuff and to avoid code duplication
      • Web UI
      • Web API

So both the UI and the API will have one or multiple services, that in turn have one or multiple repository objects.

The only reason why most examples directly query the database from the ApiController is probably because of simplicity.

like image 131
Leon Cullens Avatar answered Oct 15 '22 11:10

Leon Cullens