I have been working on a simple side project. It is a idea of View Engine that uses Database in order to get Views. It is maybe not so useful scenario, but a good one to learn more about inner workings of Asp.Net MVC and also NoSql databases. There is a big hype around NoSql. I will use RavenDB because this implementation is very close to .Net community.
First we will explore ViewEngines and HTML response generation,then we will move into the custom View Engine World. If you are interested dear reader then “Stay while and listen …”
ViewEngine
ViewEngine is responsible for HTML generation that is sent back to the client. Programmers with Asp.Net background should be familiar with WebForms Engine that uses <% %> syntax and WebControls . There are other ViewEngines like RazorEngine, which is quite popular right now, but we won’t discuss this here as it is not important for this post.
This is a very simple interface that contains Render method which has access to TextWriter that should be used to write data that will be displayed in a response sent back to client. This is almost like Render method inside WebControls.
Ok lets go back to ViewEngine.Unfortunately, or to be honest that is a very good decision, WebFormViewEngine is not directly implementing IViewEngine.
There is another layer in form of VirtualPathProviderViewEngine. This class is implementing IViewEngine and is responsible for adding “physical path” support, if you want to use files stored on your server’s file system you should create custom ViewEngine that extends this class.
ReleaseView method is a copy/paste code from Microsoft Code. As you can see this method should be used to dispose View instance.
FindPartialView and FindView methods are only returning DummyViewEngineResult. Unfortunately we had to create our own implementation of IView interface called HelloWorldView because WebFormView uses physical files and I couldn’t get it to render something.
A simple function to get page by viewname this one will be used by the engine to get the data about the page.
In DB we gonna store two pages About and Home. They will only display simple message like “Welcome to the about page”.
ViewEngine
So now the best part. How to link ViewEngine with DB.
IViewEngine interface has a definition of FindView method. This method has all the data we want right now including ViewName parameter. So let’s implement this method.
Asp.Net MVC architecture is quite easy to modyfi and extend. In this post we analysed how we can implement our different ViewEngine which is using RavenDB as a sourceof the files,