Skip to content

Variable management for policy fragments #253

@Jeroen-VdB

Description

@Jeroen-VdB

Proposal

Based on the article Variable management for policy fragments in the ms docs, it would be cool to have a functionality that can take advantage of the C# object oriented mindset to call fragments using the object itself instead of using the string identifier.

We could for example declare fragment inputs as properties decorated with [FragmentVariable], and pass values through object initializer syntax:

  // Fragment definition
  [Document(Type = DocumentType.Fragment)]
  public class MyFragment : IFragment
  {
      [FragmentVariable]
      public string HeaderValue { get; set; }

      public void Fragment(IFragmentContext context)
      {
          context.SetHeader("X-Custom", HeaderValue);
      }
  }

  // Caller
  public void Inbound(IInboundContext context)
  {
      context.IncludeFragment(new MyFragment { HeaderValue = "hello" });
  }

This should compile to:

  <set-variable name="my-fragment-HeaderValue" value="hello" />
  <include-fragment fragment-id="my-fragment" />

And inside the fragment, HeaderValue should resolve to @(context.Variables.GetValueOrDefault<string>("my-fragment-HeaderValue", "")).

[FragmentVariable] should support an optional custom variable name and a default value:

 [FragmentVariable("x-header", DefaultValue = "default")]
  public string HeaderValue { get; set; }

This of course only works for fragments called from a certain scope (api/operation/product/...) and the variables are also defined at this scope. For variables defined in other scopes, the string identifier method is still the way to go.

Component

Compiler

Contact Details

jeroen@innojero.com

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions