简体   繁体   English

MVC仅作为表示层

[英]MVC as a presentation layer only

I have a WebAPI web service, which acts as a business logic layer for client applications (WinForms and Mobile). 我有一个WebAPI Web服务,它充当客户端应用程序(WinForms和Mobile)的业务逻辑层。

Now I want to create an MVC application which will act as presentation layer only, and I am having doubts weather this architecture makes sense or does it break MVC concepts. 现在我想创建一个仅作为表示层的MVC应用程序,我怀疑这个架构是否有意义,或者它是否打破了MVC概念。

If it makes sence, what is the right/correct way of interaction between MVC application (as a presentation layer) and WebAPI service (as a business logic layer)? 如果它成功,MVC应用程序(作为表示层)和WebAPI服务(作为业务逻辑层)之间的正确/正确交互方式是什么?

Will appreciate if anyone can give me some code examples. 如果有人能给我一些代码示例,我将不胜感激。

It's fine if you use mvc this way, your controllers can access the webapi and serve the data to the templates. 如果你以这种方式使用mvc就可以了,你的控制器可以访问webapi并将数据提供给模板。

You might also consider angularjs as views/templates and the controllers there can call the webapi for data. 您也可以将angularjs视为视图/模板,并且可以将控制器调用webapi作为数据。

While I think other answers are accurate, here is some other concerns you may think of. 虽然我认为其他答案都是准确的,但您可能会想到其他一些问题。

First, your WebAPI is probably where your business are implemented. 首先,您的WebAPI可能是您的业务实施的地方。 Indeed, you may already deal with: 实际上,您可能已经处理过:

  • Business related exception 与业务相关的例外
  • Validation 验证
  • Operations available 可用的操作
  • etc. 等等

Your Api is what should not change, unless the business rule behind a certain functionnality changes too. 除非特定功能背后的业务规则也发生变化,否则您的Api不应该发生变化。

What I want to point out here is one thing: Keep your user interface completely independant from your API 我想在此指出的是一件事: 保持您的用户界面完全独立于您的API

The risk of using an MVC app with a WebApi 使用带WebApi的MVC应用程序的风险

All the code together = mutiple reasons to change the same thing 所有代码在一起=多个原因改变同样的事情

By using an MVC app, you could be tempted to package the WebApi and the MVC app in the same solution. 通过使用MVC应用程序,您可能想要将WebApi和MVC应用程序打包在同一解决方案中。 You would also be able to deploy all your stuff together. 您还可以将所有内容部署在一起。 But doing it this way, you may end up with a big bunch of code where parts are not evolving at the same speed (ie: user interface will change oftently, but do the Api will change every time a UI fix is need. NO. And do every changes to the API will impact the UI. No.) 但是这样做,你可能会得到一大堆代码,其中部件不会以相同的速度发展(即:用户界面将会发生变化,但是每次需要修复UI时,Api都会发生变化。并且对API的每个更改都会影响UI。

All code together enables shortcuts 所有代码一起启用快捷方式

What I mean by that, is that if everything is package together, a developer could be tempted to call some method directly instead of calling the API that should be the only valid facade. 我的意思是,如果所有内容都打包在一起,开发人员可能会试图直接调用某个方法而不是调用应该是唯一有效的外观的API。 Any shortcut taken can lead will lead to code duplication, bugs, validation error, etc. Again: do not package your MVC app with your API. 任何快捷方式都可能导致代码重复,错误,验证错误等。再次:不要将您的MVC应用程序与您的API打包在一起。

Solutions 解决方案

Use a Javascript framework 使用Javascript框架

The other suggestions are good. 其他建议很好。 AngularJS, ReactJS, EmberJS are good frameworks. AngularJS,ReactJS,EmberJS都是很好的框架。 (There are other, pick the one that fits your needs) But again, it will be a good choice for your architecture because you will create a clear separation between your UI app and your API app which are separated concerns. (还有其他选择适合您需求的那个)但同样,它将是您的架构的一个不错的选择,因为您将在UI应用程序和API应用程序之间创建一个明确的分离,这是分离的问题。 Your business logic will be well protected, and you will be sure that your code is only call via HTTP calls, the only valid facade of your API. 您的业​​务逻辑将得到很好的保护,您将确保您的代码仅通过HTTP调用进行调用,这是您API的唯一有效外观。 In other words, you make sure nobody will take shortcuts. 换句话说,你确保没有人会采取捷径。

Use .NET MVC app in its own project 在自己的项目中使用.NET MVC应用程序

If you still want to use .NET MVC, I would suggest that you call your API via HTTP: no shortcuts. 如果您仍想使用.NET MVC,我建议您通过HTTP调用您的API:没有快捷方式。 I would create a different solution and with a separated MVC project where calls to the API would be made using the HttpClient or something like RestSharp . 我将创建一个不同的解决方案和一个单独的MVC项目,其中使用HttpClient或类似RestSharp的方式调用API。 What you want here is to avoid to bind your UI to your API code. 您想要的是避免将UI绑定到API代码。 You want to bind your UI to the contract define by the API facade (api controllers) not their implementation. 您希望将UI绑定到API外观(api控制器)定义的合同而不是它们的实现。

I think better, of course if it possible in your situation, to use one of the JavaScript MVC frameworks. 我认为,如果可以在您的情况下使用其中一个JavaScript MVC框架 ,我认为更好 I think AngularJS, ReactJS or EmberJS will be the most coolest variants for your purpose. 我认为AngularJS,ReactJS或EmberJS将是最适合您目的的变种。 I don't think that calling ASP.MVC actions and then do another call to WEB API from there it's good idea, imho. 我不认为调用ASP.MVC操作,然后从那里再做一次WEB API调用,这是个好主意,imho。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 业务层或表示层 - business layer or presentation layer 如果我计划学习MVC,是否需要记住领域层,持久层和表示层的内容? - Are the domain layer, persistence layer and presentation layer things i need to memorize if i plan to learn MVC? 如何将数据从表示层传递到业​​务逻辑层? (ASP.NET MVC 5) - How do I pass data from presentation layer to business logic layer? (ASP.NET MVC 5) 演示文稿,业务和数据层 - Presentation, Business and Data Layer 表示层中的控件修改 - Control modification in presentation layer 如何使表示层(ASP.NET MVC)的数据访问技术(Entity Framework)无知? - How to make the data access technology (Entity Framework) ignorance from the presentation layer (ASP.NET MVC)? ASP.NET MVC3和Unity-将Unity实现与表示层分离? - ASP.NET MVC3 and Unity - Decouple Unity implementation from presentation layer? 为什么大多数ASP.NET MVC示例都直接在表示层中访问数据库? - Why do most ASP.NET MVC examples access the database directly in the presentation layer? ASP.NET MVC:如何让我的业务规则验证冒泡到表示层? - ASP.NET MVC: How can I get my business rule validation to bubble up to the presentation layer? 使业务层与演示进行通信 - Getting a business layer to communicate with presentation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM