Microsoft ASP .NET Web API,MVC 4和SPA体系结构

时间:2022-01-21 03:18:05

Microsoft recently released MVC 4 Beta, which has these new very nice features like Web API and SPA. And as always Microsoft's demos do not demonstrate best practices from software design prospective. For example, using DbController which is tightly coupled to EF.

微软最近发布了MVC 4 Beta,它具有Web API和SPA这些新的非常好的功能。一如既往,微软的演示并未展示软件设计前瞻性的最佳实践。例如,使用与EF紧密耦合的DbController。

It seems for me that SPA and Web API go hand-by-hand in modern ASP .NET app. I would like to hear any suggestions about structuring MVC 4-based solution, which is going to apply these new technologies like Web API and SPA.

在我看来,SPA和Web API在现代ASP .NET应用程序中同时进行。我想听听有关构建基于MVC 4的解决方案的任何建议,该解决方案将应用Web API和SPA等新技术。

For example, is it a good practice to separate Web API project with it's own controllers out of base MVC4 project or not. How to deal with SPA and not to use DbController in order to keep data persistence separately? What's going to be a main role of regular MVC4 app and especially Razor views?

例如,将Web API项目与基础MVC4项目中的自己的控制器分开是否是一个好习惯。如何处理SPA而不使用DbController以分别保持数据持久性?什么是常规MVC4应用程序的主要角色,尤其是Razor视图?

Any other thoughts or suggestions are highly appreciated.

任何其他想法或建议都非常感谢。

2 个解决方案

#1


4  

On separation of MVC4 + Web API: imho (as always) it depends on your concrete project.

关于MVC4 + Web API的分离:imho(一如既往)它取决于你的具体项目。

Regarding EF: you should definitely not return EF Entities but return your own DTOs instead.

关于EF:您绝对不应该返回EF实体,而是返回您自己的DTO。

The role of MVC razor views could be rendering partial views you dynamically load from the client. You also could do some stuff like conditional loading of CSS / JS etc. for the Index page being loaded initially.

MVC剃刀视图的作用可能是渲染从客户端动态加载的部分视图。你也可以为最初加载的Index页面做一些像CSS / JS等条件加载的东西。

#2


3  

I think it's a good idea to keep the API in a separate web site project from your SPA/web site, as you can run in to problems with greedy routes.

我认为将API保存在SPA /网站的单独网站项目中是个好主意,因为您可以查看贪婪路线的问题。

Definitely keep your data access separate and loosely coupled.

绝对保持您的数据访问分离和松散耦合。

#1


4  

On separation of MVC4 + Web API: imho (as always) it depends on your concrete project.

关于MVC4 + Web API的分离:imho(一如既往)它取决于你的具体项目。

Regarding EF: you should definitely not return EF Entities but return your own DTOs instead.

关于EF:您绝对不应该返回EF实体,而是返回您自己的DTO。

The role of MVC razor views could be rendering partial views you dynamically load from the client. You also could do some stuff like conditional loading of CSS / JS etc. for the Index page being loaded initially.

MVC剃刀视图的作用可能是渲染从客户端动态加载的部分视图。你也可以为最初加载的Index页面做一些像CSS / JS等条件加载的东西。

#2


3  

I think it's a good idea to keep the API in a separate web site project from your SPA/web site, as you can run in to problems with greedy routes.

我认为将API保存在SPA /网站的单独网站项目中是个好主意,因为您可以查看贪婪路线的问题。

Definitely keep your data access separate and loosely coupled.

绝对保持您的数据访问分离和松散耦合。