Question

With the Roslyn CTP out in the open, we can try to think of cool things we can do with it other than writing c# scripts.

Since asp.net mvc allows you to write custom controller factories, could we write a factory that

  • Compiles the controllers from the source file on-the-fly
  • Load the newly created type in to the runtime (can the runtime handle that?)
  • And overrule the previous type if previous requests for the same controller have already been made

This would allows us to quickly prototype or change controllers as you can simply write the code and the controllerfactory would pick up the changes without the need to recompile.

Was it helpful?

Solution

I`ve already implemented runtime controllers using monos compiler as a service some months ago.

You can find the infos and link to the code on github here: http://www.fusonic.net/en/blog/2011/04/01/scripting-asp-net-mvc-controllers-at-runtime/

Although it looks really nice at the beginning, it is not that super useful in the real-world because of several limitations.

First of all Visual Studio stands in the middle of you and your code because it strictly locks and forbids changing source code while debugging. So the only way to edit your source-files is by running without a debugger attached.

Second problem is that it is (obviously) limited to controllers. As soon as you start editing your controllers at runtime, you want a real scripting environment! For example to change your Models, and Services and whatever code you have in your project at runtime.

Then you have the problem that you can`t debug your dynamic code and so on ...

I have stopped using it after some days because it is just not a complete solution. I`m dreaming of a solution for .net where the whole mvc-project can be dynamically compiled at runtime, not limited to controllers (like a real scripting framework) and including debugging.

In the java-world there is a solution for this: http://zeroturnaround.com/jrebel/

OTHER TIPS

I don't know that much about MVC controller factories, but yes, it sounds like something that could be done. Step 2 could be done with a standard Assembly.Load() of the emitted code. Roslyn also has the ability to compile to a dynamic method if possible, which is even lighter weight.

For a related example, take a look at the prototype Razor view engine that David Ebbo put together.

Take a look at ScriptCs. I believe sooner it will be possible do things like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top