Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Add C# Classes in .Net Core App on the fly

Open gitamj opened this issue 8 years ago • 1 comments

I really like the great feature of adding inline handler methods in razor pages. Then these pages gets compiled at runtime and we can add pages to a running website on the fly.

As web developers, we might think this is not the best practice to write inline code but from another point of view, this is really an awesome way of adding new plugins and APIs to an existing web app (if the app was designed for it). For example, if we need to design a wordpress like system where plugins can be added without a site restart. If we were using MongoDb/Elastic search/Redis, we can use them without needing to recompile the app. The plugins would be able to add collections in database and call other APIs etc.

Now, the issue is, .net core only support adding razor pages this way, we cannot added C# classes like this. If there was common code between razor pages and we put it in classes, the classes have to be compiled into a DLL and deployed to the web app which requires a new publish.

But let's say if it is possible for .net to find classes on the fly, it can be an absolute pleasure to make systems where other components can be added just by copying a folder (containing pages and classes).

if .net core could detect new classes added to a running app and compile them into a "virtual DLL", the razor pages then use the virtual DLL to get the classes they need.

Or it could be a folder based approach where each class can be placed in a separate physical file, and can be found at runtime by it's namespace (which will be according to the folder structure e.g. ProjectNamespace.Pages.Test.HelperClass.cs )?

Ultimately, this approach could enable us to create a .net core app just by compiling Program.cs and Startup.cs into a DLL and rest is added on the fly.

I am not sure if you guys would like the idea or it is possible to implement it in .net core. But this is just a thought.

gitamj avatar Jan 24 '18 02:01 gitamj

Thank you for this suggestion! Instead of dropping code into the running app we would recommend using a module system, like the one Orchard Core provides. Orchard Core modules allow you to build modules as NuGet packages that can be added to your application on the fly. And you can use them as a low level framework without adopting the entire Orchard CMS infrastructure. Check out https://github.com/OrchardCMS/OrchardCore.Samples to see what this looks like.

danroth27 avatar Jan 24 '18 18:01 danroth27