Geeks With Blogs

News

Len Smith's Facebook profile


nIgnuSharp.NET //len smith is a software developer in ann arbor, mi

For the past four months my controllers have had two constructors: one that received my dependencies and one parameterless that called Windsor directly, because parameterless constructors are a requirement for the default MVC ControllerFactory.  (These constructors did have a huge //HACK comment on them.)

Wiring up IOC with zero friction was quite simple.

First, we need to make a new ControllerFactory.  In it, we will ask Windsor to give us the controller.

public class CastleWindsorControllerFactory : DefaultControllerFactory { public override IController CreateController(RequestContext context, string controllerName) { return Container.Resolve<IController>(controllerName); } }

 

Then we just need to set this as the default ControllerFactory in our global.asax:

protected void Application_Start() { RegisterRoutes(RouteTable.Routes); ControllerBuilder.Current.SetControllerFactory(new CastleWindsorControllerFactory());

 

Then, if you like friction, you can stop here...  you'll just need to add a line to your Windsor config everytime you add a new Controller.  You will forget to do this and your code will break every time you add a new Controller.

If you want to never think about your controller's IOC again, we can use Binsor and programmatically register all of our controllers using reflection. 

For every type in our Web assembly, we'll see if a type is a controller. If it is a controller, we'll register it.

for type in Assembly.Load("Web").GetTypes(): if typeof(IController).IsAssignableFrom(type): Component(type.Name.Replace("Controller", String.Empty), IController, type, LifestyleType.Transient)

 

Don't forget to include LifestyleType.Transient.  By default, Windsor will return a singleton.  We do not want our controllers to be singletons.

And that's it.  You can just start creating controllers and never have to worry about wiring up their dependencies again.

Posted on Monday, December 15, 2008 11:59 AM | Back to top


Comments on this post: Frictionless IOC in ASP.NET MVC with Windsor/Binsor

# Software
Requesting Gravatar...
If you are in need of professional assistance from companies specializing in the field of IT development, we recommend that you pay close attention to the company called Skydex Soft Ltd. You can review the information on this company on its official website at www.skydexsoft.com, where you will be able to find some information on the company itself, the list of the services that it provides, and all the necessary contact information. It should be noted that, among other types of services, the company Skydex Soft Ltd offers outsourcing services. Among its customers are companies located all over the world: in North America, Western Europe, and the Pacific Ocean region. The extensive list of the company’s customers testifies to the level and scope of the professional business activities of the company Skydex Soft Ltd. The company specializes in developing projects on Microsoft .NET platforms, including ASP.NET. The company’s staff includes many talented designers that develop truly ingenious projects. To the customers interested in the SEO optimization, the company will be able to offer numerous various solutions to this task. You will be favorably impressed with the individual approach to each and every customer. There will be a preliminary discussion organized with you as a prospective customer, during which you will have the opportunity to express all your preferences and requirements with regard to the implementation of the project, and you will also receive all the information on the completion dates and the costs involved in the project. Speaking of costs and the prices of the company’s services, they are favorably different from those offered by other companies. Working with Skydex Soft Ltd, you will be pleasantly surprised by the company’s prices, which are considerably lower than those of its competitors. For example, it is very profitable to use China for the purposes of outsourcing, as the financial expenses involved will be significantly lower than those involved in outsourcing in India or Russia. You can make an inquiry about prices at any time, and the company will provide you will all the required information pertaining to its prices. You can also review samples of the design work carried out by the company Skydex Soft Ltd. You will be able to see for yourself that only true and highly qualified professionals work at this company! The work of the customer support department is of outstanding quality. You will always receive a prompt answer to any of your questions and the solution to any problem or issue that may arise in the course of work. The company Skydex Soft Ltd ensures strictest confidentiality and safe protection of intellectual property. There are never any problems that arise in that regard. The management of the company realizes that it is one of the cornerstones of successful collaboration with its customers and, therefore, attaches much significance to the matter of observing strict confidentiality and guaranteeing protection of all intellectual property rights of its customers. An additional advantage offered is the product support in the English language provided by the company.

all for you! Gail
Left by Gail Visconsi on Aug 17, 2009 12:34 PM

# re: Frictionless IOC in ASP.NET MVC with Windsor/Binsor
Requesting Gravatar...
ASP.NET is more easier than other development technologies and languages.
Left by Giochi di Cucina on Mar 25, 2011 12:46 PM

Your comment:
 (will show your gravatar)


Copyright © Len Smith | Powered by: GeeksWithBlogs.net