Skip to content
Michael Ahern edited this page Jul 8, 2013 · 1 revision

IfFastInjector is intended to be easy to use, especially to those experienced with other IoC containers. With one exception, everything may be done with either explicit or implicit bindings. Explicit bindings are where you create a .Bind<> statement before attempting to resolve a class, while implicit bindings are resolved as needed.

Throughout the documentation I will give both the implicit and explicit versions of how to do things.

Creating an Injector

The injector library is instance based, meaning that you may have multiple independently operating injectors. If you wish to use a single static copy, that choice is left to you.

To instantiate a new injector:

using IfFastInjector;

IfInjector injector = IfInjector.NewInstance();

Type Binding

The theory of type binding, is that you have a 'key-type' (which is used for type resolution) and an implementation or concrete type which is actually instantiated.

Explicit Type Binding Example

Bind an interface or base type to a derived type

using IfFastInjector;

interface IMyType {}
class MyType : IMyType {}

class MyBaseType {}
class MyImplType : MyBaseType {}

// binding
var injector = IfInjector.NewInstance();
injector.Bind<IMyType, MyType>();
injector.Bind<MyBaseType, MyImplType>();

// usage
IMyType mt = injector.Resolve<IMyType>();
MyBaseType mbt = injector.Resolve<MyBaseType>();

Bind a type to itself

using IfFastInjector;

class MyType {}
class MyType2 {}

// binding
var injector = IfInjector.NewInstance();
injector.Bind<MyType>();
injector.Bind<MyType2 , MyType2 >(); // Equivalent to above statement

// usage
MyType mt = injector.Resolve<MyType>();
MyType2 mbt = injector.Resolve<MyType2>();

Implicit Binding Example

Bind an interface or base type to a derived type

using IfFastInjector;

[IfImplementedBy(typeof(MyType))]
interface IMyType {}

class MyType : IMyType {}

[IfImplementedBy(typeof(MyBaseType ))]
class MyBaseType {}

class MyImplType : MyBaseType {}

// binding
var injector = IfInjector.NewInstance();
injector.Bind<IMyType, MyType>();
injector.Bind<MyBaseType, MyImplType>();

// usage
IMyType mt = injector.Resolve<IMyType>();
MyBaseType mbt = injector.Resolve<MyBaseType>();

Implicit Type Resolution

Implicit binding allows you to register one type, but then have objects that request injection of a base type automatically resolve. The one interesting feature of this is that

Clone this wiki locally