-
Notifications
You must be signed in to change notification settings - Fork 0
Type Binding
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>).
For the following examples, this code is presumed:
using IfFastInjector; interaface IMyType {} class MyType : IMyType {} IfInjector injector = Injector.NewInstance();
injector.Bind<MyType>();
injector.Bind<MyIFace, MyType>();
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>();
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();
var x = injector.Resolve<IMyType>(); // Return instance of MyType
var x = injector.Resolve<MyType>(); // Return instance of MyDerivedType
var x = injector.Resolve<MyDerivedType>(); // Return instance of MyDerivedType