Grax Coding

Sunday, January 18, 2015

fFastInjector 1.0.1 is here. Brace yourself!

fFastInjector 1.0.1 is here!  (I skipped the 1.0.0 release since that programs with that version number always seems to have.)

Awesome! What is it?

fFastInjector is one of the fastest and smallest dependency injectors available for .NET.  It is available as a portable class library that targets .NET framework 4, Windows 8/8.1, and Windows Phone 8.1 or as a file that compiles into your dll.

Version 1.0.1 features

Version 1.0.1 adds generic resolution, so that if we specify IEnumerable<T> resolves to List<T>, and we ask for a resolution for type IEnumerable<Cat> we will get instance of List<Cat>.

Additionally, it adds constrained matching, so that I can specify that IEnumerable<Cat> can return a different type than IEnumerable<Dog>.

How do I Start Using It?

fFastInjector comes in three NuGet packages
  • fFastInjector-Embedded
    • This package add the Injector.cs file to your project so that compiles into your dll instead of creating a dependency on an outside dll
  • fFastInjector-MVC
    • This package installs fFastInjector for MVC 5 projects.  You will need to add a call to fFastInjector.fFastInjectorControllerFactory.RegisterControllerFactory(); into your Application_Start method in global.asax
  • fFastInjector
    • Installs the dll as a reference in your project

Hey! That Constrained Generic Matching is Awesome! but I don't want to switch from NInject, Unity, Whatever

Just configure a pass-through from your generic resolution in your favorite dependency injection engine into fFastInjector.

For example, the code below configures Unity to pass through any requests for resolutions of IEnumerable<> to fFastInjector.

container.RegisterType(typeof(IEnumerable<>),
     new InjectionFactory((c, type, s) => fFastInjector.Injector.Resolve(type)));

and the following code configures fFastInjector to return a List<T> for an IEnumerable<T> unless T inherits from Animal or Cat.  Note that Cat inherits from Animal, so types that inherit from Cat will return a CatList since Cat is the more specific type.

fFastInjector.Injector.SetGenericResolver(typeof(IEnumerable<>), typeof(List<>));
fFastInjector.Injector.SetGenericResolver(typeof(IEnumerable<>), typeof(AnimalList<>), typeof(Animal));
fFastInjector.Injector.SetGenericResolver(typeof(IEnumerable<>), typeof(CatList<>), typeof(Cat));

Ewww! Why is it all Staticky?

fFastInjector uses static variables to store information about resolutions.  There is no fFastInjector instance anywhere, just static methods and variables.

fFastInjector uses a technique with generic static variables to auto-generate, store, and execute the dependency resolution.  

Since a static variable is one that exists from the moment it is created until the end of your application's lifetime and dependency resolution is something that happens over the application's entire lifetime it makes sense to store the resolution information and the generated resolver functions in generic static variables.