<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bernhard Speiser</title>
    <description>The latest articles on DEV Community by Bernhard Speiser (@speiser).</description>
    <link>https://dev.to/speiser</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F175157%2F7ecd59c7-67f6-4a63-b553-e0aea6409ead.png</url>
      <title>DEV Community: Bernhard Speiser</title>
      <link>https://dev.to/speiser</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/speiser"/>
    <language>en</language>
    <item>
      <title>Outsmarting the C# Compiler - Using Static Types as Type Arguments</title>
      <dc:creator>Bernhard Speiser</dc:creator>
      <pubDate>Thu, 16 Sep 2021 14:48:45 +0000</pubDate>
      <link>https://dev.to/speiser/c-using-static-types-as-type-arguments-3bne</link>
      <guid>https://dev.to/speiser/c-using-static-types-as-type-arguments-3bne</guid>
      <description>&lt;p&gt;In case you were not aware, the C# compiler prevents you from using static types as type arguments. Trying to use a static type will result in the compiler error &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0718" rel="noopener noreferrer"&gt;CS0718&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error CS0718:
  'StaticClass': static types cannot be used as type arguments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be demonstrated by the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StaticClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StaticClass&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;em&gt;Outsmarting&lt;/em&gt; the Compiler
&lt;/h2&gt;

&lt;p&gt;We need to move the &lt;code&gt;Do&amp;lt;T&amp;gt;&lt;/code&gt; method into a new class and instantiate an instance of that class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This alone will not &lt;em&gt;"fix" the issue&lt;/em&gt;, as calling &lt;code&gt;instance.Do&amp;lt;StaticClass&amp;gt;()&lt;/code&gt; still results in the same compiler error.&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;dynamic&lt;/code&gt; comes into play.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Do&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;StaticClass&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;instance&lt;/code&gt; is now of type &lt;code&gt;dynamic&lt;/code&gt; the compiler will not verify the &lt;code&gt;Do&lt;/code&gt; call at compile time, but at runtime. Now the code builds. Running it will print &lt;code&gt;StaticClass&lt;/code&gt; to the console, which indicates that &lt;strong&gt;only the compiler prevents a type argument from being static&lt;/strong&gt;, although the runtime can handle static types as type arguments.&lt;/p&gt;

&lt;p&gt;The need to &lt;em&gt;outsmart&lt;/em&gt; the compiler, in most cases, indicates that there is something wrong with your design. Remember, &lt;strong&gt;just because you can, doesn't mean you should.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>compiler</category>
    </item>
    <item>
      <title>Building an Express Gateway Policy</title>
      <dc:creator>Bernhard Speiser</dc:creator>
      <pubDate>Sun, 29 Aug 2021 08:36:21 +0000</pubDate>
      <link>https://dev.to/speiser/building-an-express-gateway-policy-3eo0</link>
      <guid>https://dev.to/speiser/building-an-express-gateway-policy-3eo0</guid>
      <description>&lt;p&gt;This post will show you how to build a policy (middleware) for your &lt;a href="https://github.com/ExpressGateway/express-gateway" rel="noopener noreferrer"&gt;express gateway&lt;/a&gt;. Before creating a policy, we need to create a plugin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a plugin
&lt;/h2&gt;

&lt;p&gt;To create a plugin, we need to add a folder &lt;code&gt;plugins/my-plugin&lt;/code&gt; for it and a manifest file &lt;code&gt;plugins/my-plugin/manifest.js&lt;/code&gt; with the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// plugins/my-plugin/manifest.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0.0.1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://express-gateway.io/schemas/plugins/my-plugin.json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this is done, we need to register the plugin in the &lt;code&gt;system.config.yml&lt;/code&gt; file. Depending on your file structure, the file path may contain more or less &lt;code&gt;../&lt;/code&gt; than this example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;# system.config.yml
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;plugins:
&lt;/span&gt;&lt;span class="gi"&gt;+  my-plugin:
+    package: '../../../plugins/my-plugin/manifest.js'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the policy
&lt;/h2&gt;

&lt;p&gt;Now that we have a plugin, we can start creating our policy. Add a policies folder &lt;code&gt;plugins/my-plugin/policies&lt;/code&gt; and the policy itself &lt;code&gt;plugins/my-plugin/policies/my-policy.js&lt;/code&gt; with the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// plugins/my-plugin/policies/my-policy.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-policy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://express-gateway.io/schemas/policies/my-policy.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;actionParams&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// TODO: Implement me&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;policy&lt;/code&gt; function will be the place where you implement your specific use case.&lt;/p&gt;

&lt;p&gt;Now we need to register the policy as part of the plugin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// plugins/my-plugin/manifest.js
&lt;span class="p"&gt;module.exports = {
&lt;/span&gt;  version: '0.0.1',
&lt;span class="gi"&gt;+ init: function (pluginContext) {
+   let policy = require('./policies/my-policy')
+   pluginContext.registerPolicy(policy)
+ },
+ policies: ['my-policy'],
&lt;/span&gt;  schema: {
    "$id": "https://express-gateway.io/schemas/plugins/my-plugin.json"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the policy
&lt;/h2&gt;

&lt;p&gt;Finally, we can add the policy as any other default policy in the &lt;code&gt;gateway.config.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;# gateway.config.yml
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;policies:
&lt;/span&gt;  - log
  - jwt
&lt;span class="gi"&gt;+ - my-plugin
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and use it in our pipelines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;# gateway.config.yml
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;pipelines:
&lt;/span&gt;  my-pipeline:
    policies:
&lt;span class="gi"&gt;+     - my-plugin:
&lt;/span&gt;      - log:
        - action:
            message: ${req.method} ${req.originalUrl}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are finished with our policy, but we can still enhance it a bit. &lt;/p&gt;

&lt;h2&gt;
  
  
  Adding parameters
&lt;/h2&gt;

&lt;p&gt;In the case, that you want to configure your policy (without changing the code) you can specify parameters. The syntax used is called &lt;a href="https://json-schema.org/understanding-json-schema/reference/index.html" rel="noopener noreferrer"&gt;Json Schema&lt;/a&gt;. You can have a look at their documentation for the type that you need. In this case, we add one parameter &lt;code&gt;myarray&lt;/code&gt; with the type &lt;code&gt;string[]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// plugins/my-plugin/policies/my-policy.js
&lt;span class="err"&gt;
&lt;/span&gt;  schema: {
    $id: 'http://express-gateway.io/schemas/policies/my-policy.json',
&lt;span class="gd"&gt;-   type: 'object'
&lt;/span&gt;&lt;span class="gi"&gt;+   type: 'object',
+   properties: {
+     myarray: {
+       "type": "array",
+       "items": {
+         "type": "string"
+       }
+     }
+   }
&lt;/span&gt;  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, we can pass our value from the pipeline&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;# gateway.config.yml
&lt;span class="err"&gt;
&lt;/span&gt;    policies:
     - my-plugin:
&lt;span class="gi"&gt;+       - action:
+           myarray: ['value1', 'value2']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and use it within the &lt;code&gt;policy&lt;/code&gt; function: &lt;code&gt;actionParams.myarray&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;I hope this helped you understand how to set up a policy in an express gateway pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources/Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.express-gateway.io/docs/plugins/policy-development/" rel="noopener noreferrer"&gt;https://www.express-gateway.io/docs/plugins/policy-development/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.express-gateway.io/docs/plugins/plugin-development/" rel="noopener noreferrer"&gt;https://www.express-gateway.io/docs/plugins/plugin-development/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/62183683" rel="noopener noreferrer"&gt;https://stackoverflow.com/a/62183683&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>express</category>
      <category>gateway</category>
      <category>api</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
