Skip to content
Michael Ahern edited this page Jul 8, 2013 · 4 revisions

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 (EG. you have a dictionary of <KeyType, ImplType>).

Explicit Binding

For the following examples, this code is presumed:

using IfFastInjector;

interaface IMyType {}
class MyType : IMyType {}

IfInjector injector = Injector.NewInstance();

Bind a KeyType to Itself

injector.Bind<MyType>();

Bind an ImplType to a KeyType

injector.Bind<MyIFace, MyType>();

Bind an ImplType to a Key Type, Using a Factory

var constMT = new MyType();
MyType CreateMyType() {
  return constMT ;
}

injector.Bind<MyIFace, MyType>(() => CreateMyType());
injector.Bind<MyType, MyType>(() => new MyType());  

MyIFace invoke_CreateMyType = injector.Resolve<MyIFace>();
MyType invoke_Closure = injector.Resolve<MyType>();

Implicit Binding

For the following examples, this code is presumed:

using IfFastInjector;

// BIND: Interface to concrete type
[IfImplementedBy(typeof(MyType))]
interaface IMyType {}

// BIND: Concrete type to derived type
[IfImplementedBy(typeof(MyDerivedType))]
class MyType : IMyType {}

// NO Binding defined
class MyDerivedType: MyType  {}

IfInjector injector = Injector.NewInstance();

Resolve an Interface With a Concrete Binding

var x = injector.Resolve<IMyType>(); // Return instance of MyType

Resolve a Concrete Type Which is Bound to a Derived Type

var x = injector.Resolve<MyType>(); // Return instance of MyDerivedType

Resolve a Concrete Type Which Has No Binding

var x = injector.Resolve<MyDerivedType>(); // Return instance of MyDerivedType