<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>the1mason</title>
    <link>the1mason.com/</link>
    <description>Recent content on the1mason</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sat, 20 Jan 2024 00:00:00 +0000</lastBuildDate><atom:link href="the1mason.com/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>#1 Plugin-Based Web App in Dotnet - The Idea</title>
      <link>the1mason.com/posts/modular-app-1/</link>
      <pubDate>Sat, 20 Jan 2024 00:00:00 +0000</pubDate>
      
      <guid>the1mason.com/posts/modular-app-1/</guid>
      <description>Chapters Writing those takes time. Expect to see one published per one-two weeks.
Idea, Stack
Loading plugins
PluginBase, IPlugin
Creating plugin, DependencyInjection
Controllers, Views
Hooks and Triggers - better event system
Advanced: Unit tests, unloading plugins
Introduction Have you ever heard of plugins? These are loadable libraries, extending your application.
This series of articles is an overview of my plugin-based web application prototype and mechanisms behind it&amp;rsquo;s features, as well as my thought process and decision making during development.</description>
      <content>&lt;script src=&#34;the1mason.com/js/repo-card.js&#34;&gt;&lt;/script&gt;
&lt;div class=&#34;repo-card&#34; data-repo=&#34;the1mason/Prototype.ModularMVC&#34; data-theme=&#34;dark-theme&#34;&gt;&lt;/div&gt;
&lt;h1 id=&#34;chapters&#34;&gt;Chapters&lt;/h1&gt;
&lt;p&gt;Writing those takes time. Expect to see one published per one-two weeks.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Idea, Stack&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;Loading plugins&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;PluginBase, IPlugin&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;Creating plugin, DependencyInjection&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;Controllers, Views&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;Hooks and Triggers - better event system&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;del&gt;Advanced: Unit tests, unloading plugins&lt;/del&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Have you ever heard of plugins? These are loadable libraries, extending your application.&lt;br&gt;
This series of articles is an overview of my plugin-based web application prototype and mechanisms behind it&amp;rsquo;s features, as well as my thought process and decision making during development. These articles are a step by step guide to making your own plugin-based web app prototype.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I assume some that readers have some knowledge of C# and design patterns&lt;/em&gt;&lt;/p&gt;
&lt;h1 id=&#34;problem&#34;&gt;Problem&lt;/h1&gt;
&lt;p&gt;Self-hosted web applications can solve different problems and be of use to a variety of different people with slightly different needs. For this to work, I think that such an application should provide an option to extend its functionality. This would allow other people to build an ecosystem of different extensions around it. For example, a shopping website might have plugins for different payment systems, or a comment section under the product page. For me this also means, that instead of making one feature-rich website, that would be so specific to my needs, that it wouldn&amp;rsquo;t of any use to anyone but me, I can write a bunch of smaller modules, that could be used by someone, without having to configure other modules.&lt;/p&gt;
&lt;h1 id=&#34;choosing-my-stack&#34;&gt;Choosing my stack&lt;/h1&gt;
&lt;p&gt;&lt;img src=&#34;the1mason.com/posts/modular-app/stack.svg&#34; alt=&#34;C#, MVC, HTMX&#34;&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m a dotnet developer and I write C# code for living. This project is as much of an excersise for me as it is an interesting design prototype for C#, that hasn&amp;rsquo;t been described in detail as much online.&lt;/p&gt;
&lt;p&gt;I haven&amp;rsquo;t seen such plugin-based sites written in C#. There are some projects, using plugin based architecture&amp;hellip; Well, there&amp;rsquo;s even a &lt;a href=&#34;https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support&#34;&gt;Microsoft Learn Article&lt;/a&gt; about building such an app!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Q:&lt;/strong&gt; Why would I even bother to write all these posts and making prototypes? Even more: Why would someone be interested in such post?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A:&lt;/strong&gt; You see&amp;hellip; there&amp;rsquo;s a problem: Neither &lt;code&gt;learn.microsoft.com&lt;/code&gt; nor any other webside covers dynamically updating web interface with plugins! If you want to learn about it, it&amp;rsquo;s the right place. Also just loading libraries isn&amp;rsquo;t enough because app also has to provide some ways for plugins to interact with it, which is also covered here!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;MVC with HTMX&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;ASP.NET MVC is a web framework, that incorporates the MVC design pattern and uses SSR (Server Side Rendering) to serve content. It is a perfect fit for the HTMX library. This is a compact JavaScript library that extends basic HTML by adding some custom attributes just enough to build your app around it. You can distribute events, make AJAX requests and build truly dynamic app by using as little JS as possible.&lt;/p&gt;
&lt;div style=&#34;display: flex; justify-content: center&#34;&gt;
    &lt;img src=&#34;the1mason.com/posts/modular-app/createdwith.jpeg&#34; style=&#34;height: 100px;&#34;/&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;p&gt;HTMX uses &lt;a href=&#34;https://htmx.org/essays/hateoas/&#34;&gt;Hypermedia as the Engine of Application State (HATEOAS)&lt;/a&gt; - it&amp;rsquo;s a principle that leaves all state handling to the server, providing the client only with a set of actions that it can take.
Your regular SPA will get raw data from the server (like bank balance) and based on it, it will show or hide certain actions (like we won&amp;rsquo;t show the withdrawal option if balance is 0 or less). With HATEOAS, the server just won&amp;rsquo;t give the link to withdraw money, making this action impossible in the first place.&lt;/p&gt;
&lt;p&gt;HTMX would allow this app to be extended more easily as well. Most of the modern JS frameworks require transpiling, bundling and other sorts of stuff. This means that when a plugin is installed the client is most likely will have to be rebuilt. This is slow and needs additional dependencies.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Have you heard about Blazor WASM? You can just write client code in C#!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Blazor WASM does not support dynamic loading for plugins. Because of that, plugins won&amp;rsquo;t be able to extend the client. Also it&amp;rsquo;s initial load time is stupidly slow.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;The next article will cover the following topocs: Loading plugins in runtime, creating plugin&amp;rsquo;s instances, app-plugin communication. I&amp;rsquo;ll have the link here when I&amp;rsquo;m done writing it!&lt;/p&gt;
&lt;!-- 
---

# Loading plugins

C#, being a compiled language, can&#39;t be extended as easily as interpreted languages like Pytnon or PHP. To load plugins, we will need to load precompiled libraries dynamically after the app is compiled. To do this, [Microsoft Learn Article, mentioned before](https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support) suggests using custom AssemblyLoadContext with AssemblyDependencyResolver.

&gt; Wait, wait, wait! Using... what? I&#39;m pretty sure you could just `Assembly.Load()` stuff, right?

Not so easy! If you want to build a really working plugin system, you need a way to determine: whether this assembly has dependencies or not, and what are they.  

Let&#39;s imagine, that our plugin uses `Newtonsoft.Json` library - one of the all-time most popular C# libraries. Should we load it together with the plugin and how do we find it?  
C# has a built-in mechanism to resolve dependencies. When you compile your project, aside from `Project.Name.dll` you would have `Project.Name.deps.json` file, that will include paths to all it&#39;s dependencies! That&#39;s where `AssemblyDependencyResolver` comes in. It&#39;ll find all of plugin&#39;s `.dll` dependencies and load those as well. 

&gt; And also! What it two plugins will have the same library, but conflicting versions? Would we be able to load to of the same assemblies?

No! And yes.  
`AssemblyLoadContext` is used exactly for this. Along with `AssemblyDependencyResolver`, it will create an isolated context with current assembly and all it&#39;s dependencies. This will allow multiple plugins to have same dependencies with different versions.

There&#39;s an example of such custom AssemblyLoadContext [Click Me](https://github.com/the1mason/Prototype.ModularMVC/blob/main/Prototype.ModularMVC.App/Prototype.ModularMVC.PluginBase/PluginLoadContext.cs) and also an example of this context being used [Click Me](https://github.com/the1mason/Prototype.ModularMVC/blob/main/Prototype.ModularMVC.App/Prototype.ModularMVC.PluginBase/Impl/PluginLoaders/ManifestBasedPluginLoader.cs#L89).

---


# Plugin - App interaction

So now we figured out how we load stuff. Now: how do we interact with the app from out plugins?

First, we make all plugins to referense the `Prototype.PluginBase` project. This project will provide types that both plugin and our server can understand. We&#39;ll build communication using those.  

**Dependency Injection**

Before the app is built and ran, --&gt;</content>
    </item>
    
  </channel>
</rss>
