Daniel Seidewitz
Daniel Seidewitz

Reputation: 720

Custom (value) type in azure function http trigger as route param

In DDD it is common to define you own value types, e.g. for a UserId. Now I want to bind to the value type directly in an azure function http trigger. The parameter is part of the route.

If a define the parameter as Guid this works fine.

[FunctionName("getUserSettings")]
public async Task<IActionResult> GetUserSettings(
    [HttpTrigger(AuthorizationLevel.Function, "get",
        Route = "user-settings/{userId:guid}")]
    HttpRequest req,
    Guid userId)
{
    ...
}

But now I want to use my value type, which yields the exception System.InvalidCastException: Invalid cast from 'System.String' to 'UserId'.

Is there any way to register some custom converter for my type?

[FunctionName("getUserSettings")]
public async Task<IActionResult> GetUserSettings(
    [HttpTrigger(AuthorizationLevel.Function, "get",
        Route = "user-settings/{userId:guid}")]
    HttpRequest req,
    UserId userId)
{
    ...
}

Upvotes: 0

Views: 312

Answers (1)

Yves Reynhout
Yves Reynhout

Reputation: 2990

You have to imagine that your value objects are part of the model you've come up with to solve one or more problems in a particular domain. At the boundaries though, one is usually dealing with primitives, so it makes sense to map those primitives onto your value objects. A model could very well be hosted via a plethora of mechanisms / protocols / hosts / etc. Just write the new UserId(userId) and be done with it.

Upvotes: 0

Related Questions