简体   繁体   English

n层架构中的web api控制器

[英]web api controller in n-layer architecture

In my n-tier .Net Application I got next layers: 在我的n层.Net应用程序中,我得到了下一层:

  • Data Access Layer (EF) 数据访问层(EF)
  • Business Layer (Validation & Business Logic) 业务层(验证和业务逻辑)
  • Presentation Layers (1 MVC Controller and many API Controllers) 表示层(1个MVC控制器和许多API控制器)

I found, that my Business Services only validate business objects, call CRUD DAO methods and return results to Api Controllers. 我发现,我的业务服务只验证业务对象,调用CRUD DAO方法并将结果返回给Api控制器。

So, I doubt: may be Web Api Controllers should be used as Business Services? 所以,我怀疑:可能是Web Api控制器应该用作商业服务吗?

Interesting, just answered a similar question... 有意思,刚刚回答了类似的问题......

So I woudn't do it I were you. 所以我不会这样做我是你。

Here's just a few disadvatages of the approach from the top of my head: 这只是我头脑中方法的一些不足之处:

  1. Performance - a redundant HTTP roundtrip in Web MVC project. 性能 - Web MVC项目中的冗余HTTP往返。
  2. Separation of concerns - most of the time the functionality provided by API differs greatly form UI for the same project/application. 关注点分离 - 大多数情况下,API提供的功能与同一项目/应用程序的UI大不相同。 You might want to limit the API to a few methods with a strict contract. 您可能希望使用严格的合同将API限制为几种方法。 In case you want Web API to be a layer between Web MVC and your DAL you will have to expose all functionality you need for UI as well. 如果您希望Web API成为Web MVC和DAL之间的层,您还必须公开UI所需的所有功能。 Also you might want to have different authorization and authentication mechanisms. 您也可能希望拥有不同的授权和身份验证机制。 Very often API exceptions handling is also different as well as input validation. API异常处理通常也是不同的以及输入验证。
  3. Maintanance - everytime you need to make a change required for UI only you have to make sure it doesn't brake your API clients. 维护 - 每次只需要对UI进行更改时,您必须确保它不会使您的API客户端失效。 Also API versioning is a very important topic and mixing it with most UI changes makes this process even more difficult. 此外,API版本控制是一个非常重要的主题,并将其与大多数UI更改混合在一起使得此过程更加困难。

Probably for now you application is not that complex but from the design perspective your solution is much more flexible now than it will be if you decide to put Web API between your UI and DAL layers. 可能现在你的应用程序并不复杂,但从设计的角度来看,你的解决方案现在比你决定在你的UI和DAL层之间放置Web API要灵活得多。

N-Tier applications and multi-layer are not popular among the new wave of developers. N-Tier应用程序和多层应用程序在新一波开发人员中并不受欢迎。 Keep in mind, that just because something is not popular, among a group, does not mean that it does not have merit. 请记住,仅仅因为某些事物在一个群体中不受欢迎,并不意味着它没有价值。

Pros of MVC: MVC的优点:

  • Separation of Concerns 关注点分离
  • Unit Testing 单元测试

Does a multi-layer MVC application using a Web.API have merit: 使用Web.API的多层MVC应用程序是否有价值:

I know this will be met with some discontent and disagreement. 我知道会遇到一些不满和分歧。 However, my concern is that single purpose application developers are not giving consideration to enterprise development. 但是,我担心的是单用途应用程序开发人员没有考虑企业开发。 Enterprise development, load balancing, manageable code maintenance, and true Separation of Concerns are only possible with multi-layer applications that can easily lend themselves to N-tier. 企业开发,负载平衡,可管理的代码维护以及真正的关注点分离只适用于可轻松适应N层的多层应用程序。

Many developers are operating in environments that demand that they design and implement data structures in SQL, create and maintain models and CRUD functionality, develop controllers and design good looking and friendly views. 许多开发人员在要求他们在SQL中设计和实现数据结构,创建和维护模型和CRUD功能,开发控制器以及设计美观和友好视图的环境中运行。 The MVC model utilizing Entity Framework makes this a manageable task for these small to moderate platform developers. 利用Entity Framework的MVC模型使这对于这些从小到中的平台开发人员来说是一个可管理的任务。

In the Enterprise, separating the Business and Data Access layers from the User Interface makes real good sense. 在企业中,将业务和数据访问层与用户界面分离是非常有意义的。 Right now MVC is a popular and very functional platform for efficient and usable User Interface development. 现在,MVC是一个流行且功能强大的平台,用于高效和可用的用户界面开发。 What will be the UI platform in ten years? 十年内UI平台将会是什么? Separating the UI from the other layers gives more life to the work spent developing the business logic. 将UI与其他层分离可以为开发业务逻辑所花费的工作提供更多的生命。 Not to mention, that it allows for accessing data on multiple platforms today. 更不用说,它允许今天访问多个平台上的数据。

Multi-layer MVC using Web.API has these advantages: 使用Web.API的多层MVC具有以下优点:

  • True Separation of Concerns 真正的关注点分离
  • Supports Unit Testing 支持单元测试
  • Makes the Business logic and Data Access logic more scalable and reusable than MVC alone. 使业务逻辑和数据访问逻辑比单独的MVC更具可伸缩性和可重用性。
  • Supports data processes without page refresh. 支持数据流程而无需刷新页面。 (REST) (休息)

CONS: - Does not easily support use of Entity Framework. 缺点: - 不容易支持使用Entity Framework。 (Which is not ready for the enterprise yet anyway) (无论如何还没有为企业做好准备)

You could have your code as part as your model, and that would even be considered as good separation of concerns since MVC is build for that. 您可以将代码作为模型的一部分,这甚至可以被认为是关注点的良好分离,因为MVC就是为此而构建的。

But what my preferred thing to do is keep logic in a Business Layer of it's own. 但我最喜欢做的是将逻辑保留在自己的业务层中。 The reason for that is, I think, better separation of concerns. 我认为,原因是更好地分离关注点。 I like using IoC, so there might be different configurations that I route thought different running solutions. 我喜欢使用IoC,因此可能会有不同的配置我认为不同的运行解决方案。 Or, I might have another UI/API/Project that uses the same logic layer. 或者,我可能有另一个使用相同逻辑层的UI / API / Project。

As for the overhead, it has a little overhead, but I think worth the trouble comparing to the actual overhead in code it creates. 至于开销,它有一点开销,但我觉得与它创建的代码中的实际开销相比有点麻烦。

I agree with others here, looking into using strongly typed views, the controllers would only create an instance of the viewmodel and send it on to the view. 我在这里同意其他人,考虑使用强类型视图,控制器只会创建一个viewmodel实例并将其发送到视图。 The view model then is the intermediary to the data services layer. 然后,视图模型是数据服务层的中介。 I personally create all my entities using EF in a different project just to separate function. 我个人在不同的项目中使用EF创建我的所有实体,只是为了分离功能。 The view model can either call EF directly or you can add another layer of pre-canned EF queries which the Viewmodel uses just to populate the view collections. 视图模型可以直接调用EF,也可以添加另一层预先封装的EF查询,Viewmodel只使用它来填充视图集合。 This would look like this: 这看起来像这样:

[View]-[Controller]-[Viewmodel]-[Optional repository and interface to EF]---[EF] [查看] - [控制器] - [Viewmodel] - [EF的可选存储库和接口] --- [EF]

In the interface to EF you would catch all DB errors when trying to get information and post back to the view according to your design. 在EF的界面中,您可以在尝试获取信息时捕获所有数据库错误,并根据您的设计回发到视图。

The beauty of strongly typed views is that they post back and forth from the View and can contain methods which you can call at will. 强类型视图的优点在于它们可以从View中来回发布,并且可以包含您可以随意调用的方法。 Because they are a pseudo model for the view, they can also have properties specific to the view which may be used at the business layer. 因为它们是视图的伪模型,所以它们还可以具有特定于视图的属性,这些属性可以在业务层使用。 I pass view models around quite a bit were warranted. 我通过视图模型有相当多的保证。 (After all they are just a pointer)... (毕竟它们只是一个指针)......

You can implement business logic/validation/calucations in the actual model/entity classes rather than ApiControllers, otherwise you will end up with so much code in your controller which is not really a good thing. 您可以在实际的模型/实体类而不是ApiControllers中实现业务逻辑/验证/ calucations,否则您将在控制器中获得如此多的代码,这实际上并不是一件好事。

Also you can use DataAnnotations Attributes to perform your validation which sits outside of your controller. 您还可以使用DataAnnotations属性来执行位于控制器外部的验证。 for.eg http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx for.eg http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx

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

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