Geeks With Blogs
Aftab Qauder

Part 2 – Creating the controller factory

[Download the code as a .Zip here]

 


As we are are storing the controllers in a separate assembly we need to give the system some way of locating them. When a path is requested MVC will query the controller factories and request the relevant controller.

To allow us to map to our custom views we will create a custom controller that will look in the Windsor container for a view and if it exists return it.

In the global.asax file we create the container and then tell it to install Windsor installers in a specific directory. In the example below we are pointing it to the directory we created earlier as the output directory for the class library



 

In the assembly we create a Windsor Installer which implements IWindsorInstaller. This registers all the controllers and views. register all the views. If we look at the section which registers the views we can see it says:

“Register all types  that exist in this assembly that implement IController, for each of the found controllers give it a name tag based on the classtype and specify the lifestyle of it as transxient”

 

In this example we are giving each controller a name due to the fact that we resolve controllers by name , but we'll come to that later.

At this point if we ran the project we would see all the controllers found in this assembly were in the container

 






Our Controller factory is pretty simple apart from one fact. Typically when you create a ControllerFactory you will locate the controller in GetControllerInstance. However in this example MVC doesn’t know about the types so the parameter controllerType is null.

So to locate the controller we use the controller name. This is why we registered each controller with a name earlier.




Finally we tell MVC to use this controller factory in the Global.asax file





At this point we’ve done enough to route requests to the correct controller, however MVC still won’t know how to load the views.

 

In section 3 we will look at creating a custom view engine to locate our views.

[Part 3 - View Engine]

 

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


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

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


Copyright © aftabq | Powered by: GeeksWithBlogs.net