5

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.

2
  • 2
    This question is a little subjective Commented May 18, 2012 at 13:38
  • It might be useful if you provide a link to the example u r looking at. Commented May 18, 2012 at 14:32

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Is your Web API another MVC4 Project?
Yes, usually I create separate projects for everything. So my solution might look like this: - Context (holds the EDMX) - Repositories - Services (service layer) - Entities - ViewModels - WebUI - WebAPI - DependencyInjection (because the DI kernel must be called from both the API and UI projects)
Thank you for the continuous reply. I think I am getting a clearer picture now. Your "For instance" really clarify the whole structure for me. One more thing: when the UI calls .ajax. Since ApiController is now in a different project, it appears to be: $.ajax({ url: '**http:localhost:{port}**/Api/Outfitters/List', ... This url is ugly. And it will be different when deployed. How do you handle it?
I use a similar architecture for a project here, and while it works OK, it's worth stating that there isn't a single architecture solution you should employ. If for example your project is quite small, you could do away with a repo + service layer and query directly from your controller. It all depends on the project. I guess I'm trying to say Only add layers of complexity if they solve a problem or help you out.
This seems a very good solution, is there maybe any example of such a project somewhere on the web. All the example solutions I have found have there api's inside the same projects as the asp.net mvc application. While I have to use the web-api from within the mvc application.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.