fFastInjector - World's Fastest Dependency Injector

(2013-6-16: See updated lifestyle section for details on singleton lifestyle)

What is it?

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.

Why is it awesome?

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.

How do I use it?

Add to your  project using nuGet's Install-Package command
Install-Package fFastInjector

Set resolver for interface

fFastInjector.Injector.SetResolver<MyInterface, MyConcreteClass>();

Resolve interface.  If no resolver is set for an interface, an exception is thrown.

var result = Injector.Resolve<MyInterface>();

Resolve concrete class.  If no resolver is set for a concrete class, this class will be resolved by looking for the constructor with the fewest parameters (preference given to the parameterless constructor).  If there are parameters, fFastInjector will attempt to resolve them as well.  If they cause an infinite loop with their dependencies, fFastInjector will throw an exception.

var result = Injector.Resolve<MyConcreteClass>();

Resolve concrete class using a specific expression.

fFastInjector.Injector.SetResolver<MyConcreteClass>(() => new MyConcreteClass(new Dependency()));
var result = Injector.Resolve<MyConcreteClass>();

Resolve class or interface and additionally set a property.  This first method will simply use fFastInjector to resolve the value for MyProperty based on the type of MyProperty.

     .SetResolver<MyInterface, MyTestClass>()
     .AddPropertyInjector(v => v.MyProperty);

Resolve class or interface and additionally set a property.  This second method allows you to specify and expression that will be evaluated to set MyProperty.

      .SetResolver<MyInterface, MyTestClass>()
      .AddPropertyInjector(v => v.MyOtherProperty, () => new MyPropertyClass());

Questions (that I anticipated you might ask)

Is it ready for production use?

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.

What lifestyles does it support?

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.

  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);

Dependency Injection, Inversion of Control, Service Locator, what does it all mean?

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

  1. Create a class with a constructor that has parameters consisting of interfaces to your dependencies.  
  2. Call Injector.SetResolver for each of the dependencies
  3. Call Injector.Resolve<MyConcreteClass>() as needed

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.

Improve Your Life
Improve Your Team
Improve Your Code
Software Projects
Foo Network