每个资源或更少控制器单个Web API控制器与更多的自定义操作? [英] Single Web API controller per resource or less controllers with more custom actions?

查看:127
本文介绍了每个资源或更少控制器单个Web API控制器与更多的自定义操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要揭露我的大部分业务层的方法来一个Web API项目,让更广泛的消费。

I want to expose most of my business layer methods to a Web API project to allow for broader consumption.

一个想法是让每个资源的一台Web API控制器。

One idea is to have one Web API controller per resource.

另一个想法是让每个逻辑部分业务和使用属性一个控制器路由揭露相对的方法。

The other idea is to have one controller per logical business part and use attribute routing to expose the relative methods.

我喜欢第二种方法,导致少控制器。

I like the second approach, that results to less controllers.

什么是没有一个控制器每个资源?

What are the disadvantages of NOT having one controller per resource?

其他信息:

这个API将生活在一个企业内部网,将成为那些需要从我们掌握2-3个应用程序(ERP,薪资,酒吧code e.t.c)

This API will live in an intranet and will serve support applications that need data from our 2-3 master applications (ERP,Payroll,Barcode e.t.c)

针对此API资源将是我们业务层组件中定义的业务实体。他们将简单对象和复杂的。例如:

A resource for this APi would be business entities that are defined in our business layer assembly. They will be simple objects and complex ones. Examples:


  • 库存项目

  • 客户

  • 每个客户项目

  • 电流负载pickslips

  • present生产数据

e.t.c

示例:

我要揭露,比如 GetCustomerListByArea 方法或 GetItemsListPerCategory

I want to expose methods like GetCustomerListByArea or GetItemsListPerCategory.

什么呢?我应该创建另一个控制器或使用自定义控制器动作和属性的路由,并把它在现有的控制器?

What then? Should i create another controller or use custom controller actions and attribute routing and put it in the existing controllers?

与更多的一些信息的链接:

REST主场迎战的ASP.NET Web API RPC?谁在乎;它确实两者兼而有之。

很不错的,比较旧,文章,说明我的问题。不过,这并不深究下去实际说如何orginize控制器。在区域?或者只是文件夹?

Very nice , rather old, article explaining my question. But it does not go deeper in actual saying how to orginize controllers. In areas? Or just folders?

推荐答案

这可以归结为一个简单的设计原则 - 关注(SoC)的分离,您有关于您的业务层什么功能,你想要暴露思考和创建基于控制器。

This boils down to a simple design principle - separation of concerns (SoC), you should have a think about what functionality from your business layer you want to expose and create controllers based on that.

从上面的例子,你的可能的创建一个 ProductController的 CustomerController ,其中可能暴露以下端点:

From your example above, you might create a ProductController and CustomerController, which could expose the following endpoints:

GET /api/customer/1234        # gets customer by id
POST /api/customer/create     # creates a new customer
GET /api/customer/1234/items  # gets all items for a customer
GET /api/product/9876         # gets a product by id
POST /api/product/create      # creates a new product

希望帮助...

Hope that helps...

这篇关于每个资源或更少控制器单个Web API控制器与更多的自定义操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆