-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 02 07 Single Entity Endpoints
A "single entity" endpoint enables you to get a single instance of an entity via its primary key. For example, it enables you to get one customer, one order, etc.
Here is an example of a single entity endpoint URL:
https://localhost:8086/odata/v1/Customers(CustomerNumber=1)
To generate single entity endpoints, you must set the Enable primary key endpoints
option:
-
At a Windows prompt, move to the directory with your Harmony Core solution, and type
harmonycore gui
. Then, when the GUI for the Harmony Core CLI tool opens, go to the OData screen, scroll down to theEnable primary key endpoints
option, and double-click it. -
In the "Enter new value" screen, click the diamond icon to the right of
Enable primary key endpoints
. (The diamond will change to a checkmark.) Then click OK.
-
Save the settings you made by selecting
File > Save
from the menu. -
Regenerate generated code for your solution by selecting
Codegen > Regen
from the menu.
When code generation is finished, a message will list the files that were generated.
Enabling this option causes an additional GET method to be generated in each of your controller classes. Here is what the new method looks like in Services.Controllers\CustomersController.dbl
:
{HttpGet("Customers(CustomerNumber={aCustomerNumber})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(Customer),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Get a single Customer by primary key.
;;; </summary>
;;; <param name="aCustomerNumber">Customer number</param>
;;; <returns>Returns a SingleResult indicating the status of the operation and containing any data that was returned.</returns>
public method GetCustomer, @SingleResult<Customer>
{FromODataUri}
required in aCustomerNumber, int
proc
mreturn new SingleResult<Customer>(_DbContext.Customers.AsNoTracking().FindQuery<Customer>(_DbContext, aCustomerNumber))
endmethod
Notice that the method is decorated with an HttpGet
attribute:
{HttpGet("Customers(CustomerNumber={aCustomerNumber})")}
In this case, the attribute specifies that a parameter named CustomerNumber
must be provided to access the endpoint, so the URL will look something like this:
https://localhost:8086/odata/v1/Customers(CustomerNumber=aCustomerNumber)
The method accepts a single parameter named aCustomerNumber
, and you will notice that this corresponds to the value specified in the HttpGet
attribute. This means that the value passed via the CustomerNumber
parameter in the URL will be fed into the method by the corresponding method parameter.
You will also notice that the method returns a customer entity (record) that matches that ID. This is signified by the return value of the method, in this case @SingleResult<Customer>
.
You will find similar code in the other controller classes.
-
In Visual Studio, select
Build > Rebuild Solution
from the menu. -
Check the Output window. You should see something like this:
1>------ Rebuild All started: Project: Repository, Configuration: Debug Any CPU ------ 2>------ Rebuild All started: Project: Services.Models, Configuration: Debug Any CPU ------ 3>------ Rebuild All started: Project: Services.Controllers, Configuration: Debug Any CPU ------ 4>------ Rebuild All started: Project: Services.Isolated, Configuration: Debug Any CPU ------ 5>------ Rebuild All started: Project: Services, Configuration: Debug Any CPU ------ 6>------ Rebuild All started: Project: Services.Host, Configuration: Debug Any CPU ------ ========== Rebuild All: 6 succeeded, 0 failed, 0 skipped ==========
- In Visual Studio, press F5 (
Start Debugging
) to start the self-hosting application.
Once again you should see the console window appear with a message confirming that your service is running.
- Open your browser and go to the API documentation page:
You should see that the single entity endpoints are now documented:
- Now go to the single customer endpoint, specifying the value for the primary key of an existing customer record:
You should see a JSON response that includes the full record for the specified customer.
-
Do the same with the single entity endpoints for the other four entity types:
- When you are done with your testing, stop the self-hosting application.
Enabling single entity endpoints adds endpoints to all your code-generated OData controllers, but it is possible to prevent the generation of these endpoints for certain structures. This capability is documented in Structure-Specific Endpoint Control.
Next topic: OData Query Support
-
Tutorial 2: Building a Service from Scratch
- Creating a Basic Solution
- Enabling OData Support
- Configuring Self Hosting
- Entity Collection Endpoints
- API Documentation
- Single Entity Endpoints
- OData Query Support
- Alternate Key Endpoints
- Expanding Relations
- Postman Tests
- Supporting CRUD Operations
- Adding a Primary Key Factory
- Adding Create Endpoints
- Adding Upsert Endpoints
- Adding Patch Endpoints
- Adding Delete Endpoints
-
Harmony Core Code Generator
-
OData Aware Tools
-
Advanced Topics
- CLI Tool Customization
- Adapters
- API Versioning
- Authentication
- Authorization
- Collection Counts
- Customization File
- Custom Field Types
- Custom File Specs
- Custom Properties
- Customizing Generated Code
- Deploying to Linux
- Dynamic Call Protocol
- Environment Variables
- Field Security
- File I/O
- Improving AppSettings Processing
- Logging
- Optimistic Concurrency
- Multi-Tenancy
- Publishing in IIS
- Repeatable Unit Tests
- Stored Procedure Routing
- Suppressing OData Metadata
- Traditional Bridge
- Unit Testing
- EF Core Optimization
- Updating a Harmony Core Solution
- Updating to 3.1.90
- Creating a new Release
-
Background Information