-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 02 07 Single Entity Endpoints
Single entity endpoints allow you to a single instance of an entity at a time, via its primary key. For example, "one customer", "one order", etc.
Here is an example of a single entity endpoint URL:
https://localhost:8086/odata/v1/Customers(1)
To generate endpoints that expose a single entity, by primary key, you must enable the ENABLE_GET_ONE
option:
-
Edit regen.bat and remove the rem comment from the beginning of the line, like this:
set ENABLE_GET_ONE=-define ENABLE_GET_ONE
-
Save your changes to regen.bat.
-
If you don't already have a command prompt open in the solution folder, use the Tools > Command Prompt (x64) menu option to open a Windows command prompt, and type the following command:
cd ..
-
Type the following command to regenerate your code:
regen
As the batch file executes you will see various messages confirming which source files are being generated. Look for the word DONE to indicate that all code generation tasks completed successfully.
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
.
{ODataRoute("(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 an ODataRoute
attribute decorates the method
{ODataRoute("(CustomerNumber={aCustomerNumber})")}
This time the attribute specifies that a parameter named CustomerNumber must be provided in order 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 ODataRoute
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 notice that the method also 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 all of your other controller classes.
-
Select Build > Rebuild Solution from the Visual Studio 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 the messages confirming that your service is running.
- Open your browser and go to the API documentation page:
You should see that the entity collection endpoints are now documented:
If you want to, try opening up the API documentation and trying out one of the new operations directly from the documentation. Or, continue in the browser for now.
- 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.
-
You should also be able to 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 of 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