New! PeanutButter.Utils.Windsor
If you've used dependency injection in .NET at any point in your life, you've probably heard of, or used, Castle.Windsor (
http://www.castleproject.org/) in one or more projects. Personally, I've used Autofac, Caliburn Micro's SimpleContainer and the WindsorContainer. They're all quite good -- I guess I've just fallen back on Windsor, most especially in web projects, because it's powerful and quite easy to use.
However, there are some common functions I have to perform with the container, namely:
- Registration of Controller-based classes for dependency injection on my MVC controllers
- Registration of one-to-one service-to-implementations which my code provides (which, let's face it, is probably around 95% of the registration code that we use)
As with other PeanutButter modules, PeanutButter.Utils.Windsor is born out of a desire to stop writing the same (boring) code over and over and spend more time writing new (hopefully interesting) code. As such, please welcome the following extension methods available in PeanutButter.Utils.Windsor, for an IWindsorContainer:
- RegisterAllControllersFrom(params Assembly[] assemblies)
- Searches through the provided assemblies for controllers, matching them by having the System.Web.Mvc.Controller class in their ancestry. Matching is done by class, namespace and assembly name only, so the package doesn't require that you depend on System.Web.Mvc to use it (meaning you can still take advantage of other functionality without pulling in the entire web stack).
- RegisterAllOneToOneResolutionsAsTransientFrom(params Assembly[] assemblies)
- Searches through provided assemblies for all interfaces which are implemented by one non-abstract class and registers the interface as a service resolvable to one transient instance of the non-abstract class. I find that this is what I want about 95% of the time as I'm using DI more for a testing mechanism (and to make dependency chains not my problem at run-time) than anything else.
This method will also ignore any previously-registered services, so the idea is to run it after any more specific registrations you may have such as services you may want registered as Singleton or PerWebRequest (eg EF database contexts)
Install with nuget, in the console:
install-package PeanutButter.Utils.Windsor
Thoughts? Problems?
http://github.com/fluffynuts/PeanutButter is where you can raise issues or make pull requests.