Geeks With Blogs
Aftab Qauder

 

 

In this article we will use ASP.NET MVC 3 and the Windsor Container to explore ideas around modular software design. The principal aim of this guide is to show you how to store controllers and views in a separate assembly and load them at run-time.

[Download the code as a .Zip here]

 

Part 1 - Setting up the project and environment


In this guide the solution will have two projects. One will be the main MVC3 application MVC.Modular and the second will be a standard class library that contains all the MVC assets (Views and Controllers) WebStuff.  We will place the logic that wires up the controllers and views in the global.asax file in MVC.Modular.






We will use Castle Windsor to load the assets from an assembly. However to make this example simpler we will load that assembly from a well know directory.In this case we will create  directory called “Modules” within the web application we will use this as the target for the class library.



If we look at the project settings of the WebStuff so we can see it outputs the directory above



 

Both applications will need to access the Windsor container so add references to  windsor to both projects.




Next drag the web.config file from web project to the class library. The views will use this file for the namespace lookups. Alternatively you could add the namespaces directly to the views.
You will also need to add the following references to the Class library

System.Web
System.Web.Routing
System.Web.MVC

You will also need to add a reference to System.Web.WebPages, on my machine this was located at:


c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll

We will be using complied Razor views with our view engine. As mentioned earlier these will be located in the Assembly rather than the web project. To allow the complied views to be used you will need to install an application which compiles them for you. I used the one in

Download David Ebbo’s Raxor complier

There also seems to be one by Chris van De Steeg but I used David Ebbo’s one.

Rick click your view, go to properties and set the custom tool as ‘MvcRazorClassGenerator’. This will mean this tool is run every time you save the file

 



 

You will now notice that your view will now have a code behind file.



If we look at Greeting.cshtml we can see our view is extremely simple. It contains a HTML formatting and a div containing the words “HelloWorld”.



Looking at the code behind file you can see what is executed in the view. The contents of the file is written as a literal near the bottom.


 

In part two we will look at the construction of a ControllerFactory to help MVC locate controllers in the assembly.

 

[Part 2 - Controller Factory]

 

 

Posted on Sunday, June 12, 2011 7:31 PM | Back to top


Comments on this post: MVC 3 and Castle Windsor: A modular approach - Part 1

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © aftabq | Powered by: GeeksWithBlogs.net