ZeroSharp

Robert Anderson's ones and zeros

Replacing a Class at Runtime Using Ninject and Roslyn - Part 2: The Solution

| Comments

Previously: Part 1: The Goal

The solution

The code for the example is available on GitHub.

How it looks

So here’s the Hello World page in production:

.

We navigate to the plugins view and create a new replacement for the HelloWorldGenerator:

.

Without restarting, we can return to the HelloWorld page and see that the new class is being used because the output has changed.

.

If you delete the row from the plugins page, the behaviour reverts to the original implementation (the code that was originally shipped with production).

Basic project setup

First, I created a new ASP.NET MVC 5 application. I added a HelloWorldContrroller and a View. I added a Plugin model and corresponding views. To get started I followed the tutorial here (http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started). Once I had the basics in place, I added the following NuGet packages.

Stable

  • EntityFramework
  • Ninject
  • Ninject.MVC5
  • Ninject.Conventions

Pre-release

  • Microsoft.CodeAnalysis.CSharp

The Microsoft.CodeAnalysis.CSharp is the ‘Roslyn’ package. It is still in beta, so you have to switch to the pre-release.

Next we’ll look at the dependency injection part in more detail.

Comments