So whilst I was trying to get up to speed at work and working out how a plugin based system could work. I started to think about the best way to manage my pages. I thought it would be pretty cool if you could compile them into the frontend library. After some research I came across David Ebbo’s RazorGenerator.
I decided to use this and decided to load all of my pages into Windsor on start-up and then pass them into a View Engine that I would write loosely based upon one I found whilst researching David’s implementation. It was quite simple in the end. I simply passed the Windsor container into my View Engine.
1 2 3 4 5 6 | private readonly IWindsorContainer _container; public WindsorPrecompiledViewEngine(IWindsorContainer container) { _container = container; } |
Then when a page is requested:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { string key = GetKey(partialPath, IsMobile(controllerContext)); WebViewPage type = _container.Kernel.Resolve<WebViewPage>(key); if (type != null) { return new PrecompiledView(partialPath, type, false, FileExtensions); } return null; } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { WebViewPage type; try { type = _container.Kernel.Resolve<WebViewPage>(GetKey(viewPath, IsMobile(controllerContext))); } catch (Exception) { type = null; } if (type != null) { return new PrecompiledView(viewPath, type, true, FileExtensions); } return null; } |
The code simply works out the string of the key that would have been used when the view was registered in the container. It also checks to see whether the HTTP Request came from a Mobile Device using an extension method I wrote. If so it formats the key to look for a mobile view. This was an idea I has as I thought it would be pretty cool to use jQuery Mobile to create a mobile version of pages. However this is a future idea really as it’s not worth putting the time into at the moment. Finally if it finds the view it returns it as a Pre-compiled View Object which I believe was taken from David’s example.
This is all plugged in as a Bootstrapping task which adds this View Engine to MVC:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class PrecompiledViewEngineStartupTask : IStartupTask { public void Run() { var engine = new WindsorPrecompiledViewEngine((IWindsorContainer)Bootstrapper.Container); ViewEngines.Engines.Add(engine); } public void Reset() { throw new NotImplementedException(); } } |
Future ideas for this evolve around changing views without having to restart the web server. At the moment if you want to change a page you have to deploy the front end library and restart the server. I came across this Hot Swapping concept from Ayende Rahien and believe it would be possible to do this for swapping out old views for new views in the Windsor container. This could also be possible for any of the libraries dropped into the Plugin folder and could be an interesting feature to work on in the future.
Anyway hope this was interesting!
It‘s quite in here! Why not leave a response?