|
1 | 1 | # Endpoint Types |
2 | 2 |
|
3 | | -WebResultEndpoint, BusinessResultEndpoint and ServiceEndpoint, have a 'HandleAsync' method which returns a strongly typed [business result](https://github.com/modabas/ModResults). But they differ in converting these business results into HTTP responses before sending response to client. |
| 3 | +There are 4 different endpoint types: |
| 4 | + 1. [MinimalEndpoint](EndpointTypes_MinimalEndpoint.md) is in `ModEndpoints.Core` package. |
| 5 | + 2. [WebResultEndpoint](EndpointTypes_WebResultEndpoint.md), |
| 6 | + 3. [BusinessResultEndpoint](EndpointTypes_BusinessResultEndpoint.md), |
| 7 | + 4. [ServiceEndpoint](EndpointTypes_ServiceEndpoint.md) are in `ModEndpoints` package. |
4 | 8 |
|
5 | | -MinimalEndpoint within ModEndpoints.Core package, is closest to barebones Minimal API. Its 'HandleAsync' method support the following types of return values: |
6 | | - |
7 | | -- string |
8 | | -- T (Any other type) |
9 | | -- Minimal API IResult based (Including TypedResults with Results<TResult1, TResultN> return value) |
10 | | - |
11 | | -See [How to create responses in Minimal API apps](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/responses?view=aspnetcore-8.0) for detailed information. Other features described previously are common for all of them. |
12 | | - |
13 | | -Each type of endpoint has various implementations that accept a request model or not, that has a response model or not. |
14 | | - |
15 | | -## MinimalEndpoint |
16 | | - |
17 | | -A MinimalEndpoint implementation, after handling request, returns the response model. |
18 | | - |
19 | | -- **MinimalEndpoint<TRequest, TResponse>**: Has a request model, supports request validation and returns a response model. |
20 | | -- **MinimalEndpoint<TResponse>**: Doesn't have a request model and returns a response model. |
21 | | -- **MinimalEndpointWithStreamingResponse<TRequest, TResponse>**: Has a request model, supports request validation and returns `IAsyncEnumerable<TResponse>`. |
22 | | -- **MinimalEndpointWithStreamingResponse<TResponse>**: Doesn't have a request model and returns `IAsyncEnumerable<TResponse>`. |
23 | | - |
24 | | -## WebResultEndpoint |
25 | | - |
26 | | -A WebResultEndpoint implementation, after handling request, maps the [business result](https://github.com/modabas/ModResults) of HandleAsync method to a Minimal API IResult depending on the business result type, state and failure type (if any). Mapping behaviour can be modified or replaced with a custom one. |
27 | | - |
28 | | -- **WebResultEndpoint<TRequest, TResponse>**: Has a request model, supports request validation and returns a response model as body of Minimal API IResult if successful. |
29 | | -- **WebResultEndpoint<TRequest>**: Has a request model, supports request validation, doesn't have a response model to return within Minimal API IResult. |
30 | | -- **WebResultEndpointWithEmptyRequest<TResponse>**: Doesn't have a request model and returns a response model as body of Minimal API IResult if successful. |
31 | | -- **WebResultEndpointWithEmptyRequest**: Doesn't have a request model, doesn't have a response model to return within Minimal API IResult. |
32 | | - |
33 | | -When result returned from handler method is in Ok state, default WebResultEndpoint response mapping behaviour is: |
34 | | -- For an [endpoint without a response model](../samples/ShowcaseWebApi/Features/Books/DeleteBook.cs), return HTTP 204 No Content. |
35 | | -- For an endpoint with a response model, return HTTP 200 OK with response model as body. |
36 | | - |
37 | | -Response HTTP success status code can be configured by [calling 'Produces' extension method during configuration](../samples/ShowcaseWebApi/Features/Books/CreateBook.cs) of endpoint with one of the following status codes: |
38 | | -- StatusCodes.Status200OK, |
39 | | -- StatusCodes.Status201Created, |
40 | | -- StatusCodes.Status202Accepted, |
41 | | -- StatusCodes.Status204NoContent, |
42 | | -- StatusCodes.Status205ResetContent |
43 | | - |
44 | | -When result returned from handler method is in Failed state, default WebResultEndpoint response mapping will create a Minimal API IResult with a 4XX or 5XX HTTP Status Code depending on the FailureType of [business result](https://github.com/modabas/ModResults). |
45 | | - |
46 | | -It is also possible to implement a custom response mapping behaviour for a WebResultEndpoint. To do so: |
47 | | -- Create an IResultToResponseMapper implementation, |
48 | | -- Add it to dependency injection service collection with a string key during app startup, |
49 | | -- Apply ResultToResponseMapper attribute to endpoint classes that will be using custom mapper. Use service registration string key as Name property of attribute. |
50 | | - |
51 | | -## BusinessResultEndpoint |
52 | | - |
53 | | -A BusinessResultEndpoint implementation, after handling request, encapsulates the [business result](https://github.com/modabas/ModResults) of HandleAsync method in a HTTP 200 Minimal API IResult and sends to client. The [business result](https://github.com/modabas/ModResults) returned may be in Ok or Failed state. This behaviour makes BusinessResultEndpoints more suitable for cases where clients are aware of Result or Result<TValue> implementations. |
54 | | - |
55 | | -- **BusinessResultEndpoint<TRequest, TResultValue>**: Has a request model, supports request validation and returns a [Result<TResultValue>](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
56 | | -- **BusinessResultEndpoint<TRequest>**: Has a request model, supports request validation and returns a [Result](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
57 | | -- **BusinessResultEndpointWithEmptyRequest<TResultValue>**: Doesn't have a request model and returns a [Result<TResultValue>](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
58 | | -- **BusinessResultEndpointWithEmptyRequest**: Doesn't have a request model and returns a [Result](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
59 | | - |
60 | | - |
61 | | -## ServiceEndpoint |
62 | | - |
63 | | -This is a very specialized endpoint which is intended to abstract away all HTTP client and request setup, consumption and response handling when used together with its client implementation. Aim is to enable developers to easily consume remote services with a strongly typed request and response model only by sharing said models between the service and client projects. |
64 | | - |
65 | | -A ServiceEndpoint implementation, similar to BusinessResultEntpoint, encapsulates the response [business result](https://github.com/modabas/ModResults) of HandleAsync method in a HTTP 200 Minimal API IResult and sends to client. The [business result](https://github.com/modabas/ModResults) returned may be in Ok or Failed state. |
66 | | - |
67 | | -- **ServiceEndpoint<TRequest, TResultValue>**: Has a request model, supports request validation and returns a [Result<TResultValue>](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
68 | | -- **ServiceEndpoint<TRequest>**: Has a request model, supports request validation and returns a [Result](https://github.com/modabas/ModResults) within HTTP 200 IResult. |
69 | | -- **ServiceEndpointWithStreamingResponse<TRequest, TResultValue>**: Has a request model, supports request validation and returns `IAsyncEnumerable<StreamingResponseItem<TResultValue>>`. |
70 | | -- **ServiceEndpointWithStreamingResponse<TRequest>**: Has a request model, supports request validation and returns `IAsyncEnumerable<StreamingResponseItem>`. |
71 | | - |
72 | | ->**Note**: `StreamingResponseItem` is a specialized type that contains a `Result` object and also Response Type and Id fields. It is used for streaming responses to allow clients to process each item as it arrives. |
73 | | -
|
74 | | -A ServiceEndpoint has following special traits and constraints: |
75 | | -- A ServiceEndpoint is always registered as a POST method, and its bound pattern is determined accourding to its request type. |
76 | | -- Request model defined for a ServiceEndpoint is bound with [FromBody] attribute. |
77 | | -- A ServiceEndpoint's request must implement either IServiceRequest (for endpoints implementing ServiceEndpoint<TRequest>) or IServiceRequest<TResultValue> (for endpoints implementing ServiceEndpoint<TRequest, TResultValue>) |
78 | | -- A ServiceEndpoint's request is specific to that endpoint. Each endpoint must have its unique request type. |
79 | | -- To utilize the advantages of a ServiceEndpoint over other endpoint types, its request and response models have to be shared with clients and therefore has to be in a seperate class library. |
80 | | - |
81 | | -These restrictions enable clients to call ServiceEndpoints by utilizing a specialized message channel resolved from dependency injection, see [ServiceEndpoint Clients](ServiceEndpointClients.md) documentation for details. |
| 9 | +Each type of endpoint has various implementations that accept a request model or not, that has a response model or not. Please refer to individual documents for more info about each endpoint. |
0 commit comments