MVC4 WebAPI的正确架构是什么?

Bla*_*ise 5 architecture asp.net-mvc-4 asp.net-web-api

我运行了一些搜索,但没有找到这个问题的可用答案.

好的,我的MVC 3架构是这样的:

- 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.)
Run Code Online (Sandbox Code Playgroud)

WebApiMVC4出现如此大的吸引力,因为我们将能够从其他应用程序调用的CRUD操作.我们都喜欢这个主意.

但是我发现的现有示例在MVC4项目中有CRUD操作ApiController.我习惯将这些操作放入一个单独的DAL项目中.推荐的选择是什么?我们还能有一个单独的DAL课吗?您如何设计专家的架构?

感谢您提供所有有用的建议.

Leo*_*ens 7

我这样做是:

  • 用于查询数据库的存储库
    • 服务层验证内容并避免代码重复
      • Web UI
      • Web API

因此,UI和API都将具有一个或多个服务,而这些服务又具有一个或多个存储库对象.

大多数示例直接从ApiController查询数据库的唯一原因可能是因为简单.

  • 是的,通常我会为所有事情创建单独的项目.所以我的解决方案可能如下所示: - Context(持有EDMX) - 存储库 - 服务(服务层) - 实体 - ViewModels - WebUI - WebAPI - DependencyInjection(因为必须从API和UI项目调用DI内核) (2认同)