-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial 02 09 Alternate Key Endpoints
In addition to generating OData endpoints that allow entities to be accessed by their primary key, which usually results in a single entity being returned, it is also possible to generate endpoints that access data based on alternate keys that may be defined for each entity type.
Some of these endpoints may result in a single entity being returned. For example, you might access a customer entity via a key associated with the customer's main office telephone number. Other endpoints may result in multiple entities being returned, for example via a key that enables you to access all customers in a particular city or state.
To generate endpoints that expose entities via alternate keys, you must set the Enable alt 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 alt endpoints
option, and double-click it. -
In the "Enter new value" screen, click the diamond icon to the right of
Enable alt endpoints
. (The diamond will change to a checkmark.) Then click OK.
-
Save the settings you made by selecting
File > Save
from the menu. -
Generate code for your solution by selecting
Codegen > Regen
from the menu.
When code generation is finished, a message lists the files that were generated. Click OK to close the message.
No additional files are generated as a result of this change. Instead, this option enables additional areas of code in the template files being used, which results in the generation of additional code into several source files:
-
A new GET method (endpoint) is added in the controller classes, one for each alternate key.
-
New alternate key endpoints will appear in the API documentation.
Here is an example of an alternate key endpoint method (from CustomersController.dbl
):
{HttpGet("Customers(State={aState})")}
{Produces("application/json")}
{ProducesResponseType(^typeof(IEnumerable<Customer>),StatusCodes.Status200OK)}
{ProducesResponseType(StatusCodes.Status404NotFound)}
{EnableQuery(MaxExpansionDepth=4)}
;;; <summary>
;;; Get customers by alternate key key State.
;;; </summary>
;;; <param name="aState">State</param>
;;; <returns>Returns an IActionResult indicating the status of the operation and containing any data that was returned.</returns>
public method GetCustomersByState, @IActionResult
{FromODataUri}
required in aState, String
proc
data result = _DbContext.Customers.AsNoTracking().FindAlternate("State",aState)
if (result == ^null)
mreturn NotFound()
mreturn Ok(result)
endmethod
-
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 the messages confirming that your service is running.
You should be able to interact with the additional endpoints. The new functionality comes in the form of additional GET methods in controllers. For example, if you are working with the Harmony Core sample repository and data, these are some of the additional operations that should now be available:
-
Get all customers in the state of Washington:
-
Get all items with white flowers:
-
Get all vendors with a payment terms code of '30':
- When you are done testing, stop the self-hosting application.
Enabling alternate key 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: Expanding Relations
-
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