Skip to content

Support for reusable expressions #185

@drjonnicholson

Description

@drjonnicholson

Proposal

While APIM has fragments, which allows reusability between policies, I think having the ability to define policies through C# code provides some really great opportunities to offer an alternative way to "share functionality".

Specifically, I'm thinking in scenarios where the same "line of code" might be used in multiple places. For example, say I needed to get the caller's IP address and I'm behind an App Gateway. I might do something like this:

public static string GetOriginIp(IExpressionContext context) => context.Request.Headers.GetValueOrDefault("X-Forwarded-For", context.Request.IpAddress)

This might need to be used in a number of places such as for caching, rate limiting, telemetry, request logic, etc. I could use a fragment to do nothing but set a variable into the policy and retrieve that, but that feels really heavy handed and not really hat I think fragments are there for.

Since this toolkit allows the policy to be generated, there's less concern about repetition in the output. That is, I mind less if that above line of code appears multiple times as long as I know when it appears it's the version I've defined and it's been thoroughly tested.

So what would be fantastic is the ability to define simple static classes with expression methods in them. Those "helper" classes can then group together a load of common expression logic that might be reused, can be tested like any other expression methods, referred to in the way you'd expect in the C# code, and compile out to the same XML output as if the method were defined in the current compilation unit.

In theory, that would allow more flexibility in how a solution approaches code reuse - expressions can be externalised as well tested but repeatedly injected code, fragments are kept to reusable series of policies.

Component

Compiler

Contact Details

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions