简体   繁体   English

MVC服务的最佳实践

[英]Best Practice for Services with MVC

I am new to MVC and learned the concepts. 我是MVC的新手并学习了这些概念。 But as a practical view, as a beginner, little consfused. 但作为一个实际的观点,作为一个初学者,几乎没有注意到。

MVC is a presentation pattern. MVC是一种表示模式。 But MVC also contains Controller and Model. 但是MVC还包含Controller和Model。 These are the business and data logics. 这些是业务和数据逻辑。 So how it migled in the presentation itself. 那么它是如何在演示文稿本身中迁移的。 Is my understanding right? 我的理解是对的吗?

If i implement WCF Service / Web Api how the approach will be? 如果我实现WCF服务/ Web Api的方法将如何? I read articles and forum's thread about this, but i didn't got it clearly. 我读了关于此的文章和论坛的主题,但我没有清楚地了解它。

Mostly said web api is the best combination and simply include in the controller. 大多数人说web api是最好的组合,只是包含在控制器中。 But then how the layer is seperated as presentation and service layer? 但那么该层如何作为演示和服务层分离? Here the service layer (Web api ) is specifically for the MVC application / web UI only. 这里的服务层(Web api)专门用于MVC应用程序/ Web UI。 Not getting generally. 一般都没有。

I used WCF like Service Layer as one application and Business Layer and Data Layer as class libariries. 我使用WCF,如服务层作为一个应用程序,业务层和数据层作为类libariries。 Here i can host the service in iis and access from anywherer. 在这里,我可以在iis中托管服务并从任何地方访问。 How this service is used in the MVC application? 如何在MVC应用程序中使用此服务? Simply avoid implementing controller and model and simple use view with jquery? 简单地避免使用jquery实现控制器和模型以及简单的使用视图?

Please give the correct apporach with WCF and Web API.. 请使用WCF和Web API提供正确的apporach ..

You've got several questions, so I'll try to answer all of them. 你有几个问题,所以我会尝试回答所有这些问题。

MVC is a presentation pattern. MVC是一种表示模式。 But MVC also contains Controller and Model. 但是MVC还包含Controller和Model。 These are the business and data logics. 这些是业务和数据逻辑。 So how it migled in the presentation itself. 那么它是如何在演示文稿本身中迁移的。 Is my understanding right? 我的理解是对的吗?

The controller is presentation-layer. controller是表示层。 It really should act like a coordinator and not contain any business or persistence logic. 它确实应该像协调器一样 ,不包含任何业务或持久性逻辑。 The model certainly can have business logic, but not any persistence logic. model当然可以具有业务逻辑,但不具有任何持久性逻辑。 Persistence is related to infrastructure and doesn't belong in your model or controller. 持久性与基础架构相关,不属于您的模型或控制器。

If i implement WCF Service / Web Api how the approach will be? 如果我实现WCF服务/ Web Api的方法将如何? I read articles and forum's thread about this, but i didn't got it clearly 我读了关于此的文章和论坛的主题,但我没有清楚地了解它

Your service can be separate from the MVC project, although it's acceptable to include your Web API in your MVC project. 您的服务可以与MVC项目分开,但在您的MVC项目中包含Web API是可以接受的。 Personally, I think it should be separate. 就个人而言,我认为它应该是分开的。 Keep in mind that what you've read about services in an MVC project may be referring to a local service or repository in a DDD (domain driven design) sense. 请记住,您在MVC项目中阅读的有关服务的内容可能是指DDD(域驱动设计)意义上的本地服务或存储库。 It's way different. 这有点不同。

Mostly said web api is the best combination and simply include in the controller. 大多数人说web api是最好的组合,只是包含在控制器中。 But then how the layer is seperated as presentation and service layer? 但那么该层如何作为演示和服务层分离? Here the service layer (Web api ) is specifically for the MVC application / web UI only. 这里的服务层(Web api)专门用于MVC应用程序/ Web UI。 Not getting generally 一般都没有

WCF or Web API? WCF或Web API? Choose the best one for your needs and skills. 根据您的需求和技能选择最佳的一个。 Either way, I think it's best to keep the service separate. 无论哪种方式,我认为最好将服务分开。 Again, don't confuse a DDD service with an autonomous service-layer. 同样,不要将DDD服务与自治服务层混淆。

I used WCF like Service Layer as one application and Business Layer and Data Layer as class libariries. 我使用WCF,如服务层作为一个应用程序,业务层和数据层作为类libariries。 Here i can host the service in iis and access from anywherer. 在这里,我可以在iis中托管服务并从任何地方访问。 How this service is used in the MVC application? 如何在MVC应用程序中使用此服务? Simply avoid implementing controller and model and simple use view with jquery? 简单地避免使用jquery实现控制器和模型以及简单的使用视图?

Yes, you can call the service from your view's JQuery code or from your controller, it depends on your needs. 是的,您可以从视图的JQuery代码或控制器中调用服务,这取决于您的需求。 It's probably better to create a local service in your MVC project that will contact your services and data-stores. 最好在MVC项目中创建一个与您的服务和数据存储联系的本地服务。 Then, have your controller use this local service for its data needs. 然后,让您的控制器使用此本地服务来满足其数据需求。

In a simple application, I often see two projects in a solution, An MVC project for the web front-end and a class library for data access, lets call that "core". 在一个简单的应用程序中,我经常在解决方案中看到两个项目,一个用于Web前端的MVC项目和一个用于数据访问的类库,让我们称之为“核心”。 The core project contains all of the ORM related classes and a service class implementing an interface, lets call it IService which handles business logic. 核心项目包含所有与ORM相关的类和实现接口的服务类,我们称之为处理业务逻辑的IService。

When a request comes in to the MVC controller, the controller will call IService to pull back the data it needs and pass that information onto the view. 当请求进入MVC控制器时,控制器将调用IService以撤回所需的数据并将该信息传递给视图。 In this approach there aren't any "Models" in your MVC folder, but the ORM Model is being used. 在这种方法中,MVC文件夹中没有任何“模型”,但正在使用ORM模型。 For more complex pages, you would create an MVC Model, to combine several pieces of data returned from ISservice and present them in a way that makes sense to the user. 对于更复杂的页面,您将创建一个MVC模型,以组合从ISservice返回的多个数据,并以对用户有意义的方式呈现它们。

In a larger project, the core project which contains IService may be 1-many WCF or Web API services. 在一个更大的项目中,包含IService的核心项目可能是1-WCF或Web API服务。 This gives you more flexibility for scaling the application and allows your web pages to make ajax calls directly to the data layer. 这为您扩展应用程序提供了更大的灵活性,并允许您的网页直接向数据层发出ajax调用。

In this scenario, when a request comes into the controller, it would still call IService, but this service is actually a WCF or Web API service. 在这种情况下,当请求进入控制器时,它仍然会调用IService,但此服务实际上是WCF或Web API服务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM