(2013-6-16: See updated lifestyle section for details on singleton lifestyle)
fFastInjector is the world’s fastest .NET dependency injector/service locator.
In order to prove that it is the fastest, I downloaded the code that accompanies this blog post and added an adapter for fFastInjector. In my tests, it beat every other dependency injector on the list.
fFastInjector is extremely fast and lightweight. It is the fastest and one of the smallest dependency injectors out there. It is easy to add to your project using nuGet, set up a few mappings and be on your way.
Install-Package fFastInjector
fFastInjector.Injector.SetResolver<MyInterface, MyConcreteClass>();
var result = Injector.Resolve<MyInterface>();
var result = Injector.Resolve<MyConcreteClass>();
fFastInjector.Injector.SetResolver<MyConcreteClass>(() => new MyConcreteClass(new Dependency()));
var result = Injector.Resolve<MyConcreteClass>();
fFastInjector.Injector
.SetResolver<MyInterface, MyTestClass>()
.AddPropertyInjector(v => v.MyProperty);
fFastInjector.Injector
.SetResolver<MyInterface, MyTestClass>()
.AddPropertyInjector(v => v.MyOtherProperty, () => new MyPropertyClass());
There’s not a lot of code and my tests have 100% code coverage.
I do recommend it for production use, assuming you have a proper test phase before you go to production. Once I have used it in production for a reasonable time, I will release the 1.0 version.
Hippy, banker, or metalhead. Kidding. fFastInjector supports Transient and Singleton lifestyles. No lifestyles are specified, but provided your resolver calls “new” for transient lifestyles and just returns an existing instance for the singleton lifestyle, all should be well. I will be looking into other lifestyles but I expect them to be built as add-ons instead of in the base fFastInjector. I expect to build an add-on to support asp.net/MVC that may support a single-request lifestyle and automatically configure controller resolution.
Update:
The next release adds a command to specifically set a singleton resolver. Until that is available, create a static variable, instantiate your singleton, and use the following to resolve:
myStaticVar = new MyConcreteSingleton();
fFastInjector.Injector.SetResolver<IMyInterface>(() => myStaticVar);
Inversion of control is the technique of making object dependencies known at run-time instead of compile-time. This is accomplished by creating a contract or interface between objects and then using a run-time tool, such as fFastInjector, to determine the dependency for that contract.
Dependency injection is one technique for inversion of control. The class is created with injection points in the constructor or properties and then those dependencies are injected when the class is constructed. Classes built using this technique have no dependency on the dependency injector,
To use fFastInjector this way
Service location is a technique where dependencies are resolved as needed. The class calls the service locator with the interface needed and the service locator returns a concrete reference for that interface.
To use fFastInjector this way, call Injector.Resolve<IMyInterface>() anywhere in your code that you need IMyInterface.