Asp.net MVC2 provides many extensions. With these extensions, Asp.net MVC is more flexible to use. Simone chiaretta once wrote an articleArticle: 13 ASP. net mvc extensibility points you have to know. The article briefly introduces the extensions that must be known by 13 Asp.net MVC developers. I will select several important extension points from these 13 extension points for detailed introduction. This article will first introduce controller factory.
Controller factory is an important extension point in the ASP. MVC framework. Controller factory is used to create a controller. One of the most common scenarios of controller is to support dependency injection. However, if dependency injection is directly used in the controller, the Asp.net MVC framework will not create a controller for you by default, and you will get the following error, for example:
The cause of the above error is that defaultcontrollerfactory uses activator. createinstance to initialize the controller, and this method does not know anything about the injection-dependent parameters.
In this case, we need to use the custom controller factory.
A custom controller factory must inherit icontrollerfactory or icontrollerfactory classes, such as defaultcontrollerfactory. BelowCodeIs a custom controller factory.
Public classMycustomcontrollerfactory:Defaultcontrollerfactory{Protected overrideIcontrollerGetcontrollerinstance (RequestcontextRequestcontext,TypeControllertype ){/* Implement controller creation logic */Return Base. Getcontrollerinstance (requestcontext, controllertype );}}
After defining the Controller factory, register it in application_start:
Controllerbuilder. Current. setcontrollerfactory (NewMycustomcontrollerfactory());
In this way, the MVC Framework will use your custom controller factory to create the controller.
See the productscontroller below. This controller accepts dependency injection in the constructor.
Public classProductscontroller:Controller{PublicProductscontroller (iproductrepository repository, ishippingcalculator shippingcalculator, itaxservice taxservice ){/*...*/}/*...*/}
It is very boring to manually write code to create such a controller. Fortunately, we don't need to write our own code. There are many IOC tools for us to use. One of the best dependency injection tools in. NET is structuremap, which is a lightweight open-source free IOC framework. Below I will use this tool to implement a simple MVC dependency injection demo.
1. Download this tool at http://structuremapsourceforge.net. Reference structuremap. dll in the MVC project.
2. Define an imessageprovider interface for dependency injection in models and an implementation class of it.Structuremapmessageprovider.
NamespaceMvcapplication1.models {Public interfaceImessageprovider{StringGetmessage ();}Public classStructuremapmessageprovider:Imessageprovider{Public StringGetmessage (){Return"This message was provided by structuremap";}}}
3. Use structuremap to customize the Controller Factory Code as follows:
Public classStructuremapcontrollerfactory:Defaultcontrollerfactory{Protected overrideIcontrollerGetcontrollerinstance (RequestcontextRequestcontext,TypeControllertype ){ReturnObjectfactory. Getinstance (controllertype)AsIcontroller;}}
4. Define a static structuremapbootstrapper class to initialize structuremap and set structuremapcontrollerfactory as the controller factory of the project. The Code is as follows:
Public static classStructuremapbootstrapper{Public static voidInitialize (){Objectfactory. Initialize (x => X. addregistry (NewMystructuremapapplicationregistry()));}Public static voidSetcontrollerfactory (){VaRControllerfactory =NewStructuremapcontrollerfactory();Controllerbuilder. Current. setcontrollerfactory (controllerfactory );}}
5. Use the structuremapbootstrapper class above in global. asax to set the Controller factory and initialize structuremap.
Structuremapbootstrapper. Setcontrollerfactory ();Structuremapbootstrapper. Initialize ();
6. In this way, we can use it normally. We set homecontroller as follows:
[Handleerror]Public classHomecontroller:Controller{PrivateImessageprovider_ Messageprovider;PublicHomecontroller (ImessageproviderMessageprovider) {_ messageprovider = messageprovider ;}PublicActionresultIndex () {viewdata ["Message"] = _ Messageprovider. getmessage ();ReturnView ();}}The running result is as follows:
Summary:This article briefly introduces the custom controller factory. The next article will introduce modelbinder.
reference: Asp.net MVC 2 in action