This project has moved and is read-only. For the latest updates, please go here.
Portable IOC is tiny, lightweight, and easy to use. That's why there's not a ton of documentation - at under 20KB with less than 200 lines of source code, the program itself provides most of the documentation. To get started, create an instance:

var pIoC = new PortableIoc();

Register the implementation for a type. The registration takes a delegate. The signature for the delegate is a function that receives the IoC container (this allows your function to resolve other dependencies) and returns an instance of the target type (this can be anything assignable to the base type, and may point to a factory method or even a static object if you want).

pIoC.Register<IBar>(ioc => new SimpleBar());

To resolve the implementation (by default, this will return a shared instance, so subsequent calls will return the same object):

var actual = pIoC.Resolve<IBar>();

To test to see if you are able to resolve a type:

var canResolve = pIoC.CanResolve<IBar>();

To try to resolve a type that may not be defined:

 IBar barInstance;
 var result = pIoC.TryResolve(out barInstance); 

To resolve a named instance (allows for different implementations for the same type):

 pIoC.Register<IBar>(ioc => new ComplexBar()); 
 pIoC.Register<IBar>(ioc => new SimpleBar(), "SomeInstance");
 var result1 = pIoC.Resolve<IBar>(); // ComplexBar 
 var result2 = pIoC.Resolve<IBar>("SomeInstance"); // SimpleBar 

To request a non-shared instance:

 pIoC.Register<IBar>(ioc => new SimpleBar());
 var actual1 = pIoC.Resolve<IBar>();
 var actual2 = pIoC.Resolve<IBar>(); // this is the same as actual1
 var actual3 = pIoC.Resolve<IBar>(true); // this will be different than actual1 and actual2 

To unregister a definition (will also destroy any shared instance):

var success = pIoC.Unregister<IBar>(); // true if previously registered, otherwise false

To destroy the shared instance so that subsequent calls receive a new shared instance:

 pIoC.Register<IBar>(ioc => new SimpleBar());
 var actual1 = pIoC.Resolve<IBar>();
 var actual2 = pIoC.Destroy<IBar>(); // only destroys in the container, doesn't affect actual1
 var actual2 = pIoC.Resolve<IBar>(); // now this is different than actual1 

What if the type is unknown at runtime for a dynamically instantiated singleton?

 pIoC.Register<object>("RuntimeType", ioc => Instantiate());
   dynamic myInstance = pIoC.Resolve<object>("RuntimeType"); 

To declare a type with constructor injection:

  pIoC.Register<IBar>(ioc => new SimpleBar());            
 pIoC.Register<IFoo>(ioc => new SimpleFoo(ioc.Resolve<IBar>()));
 var actual = pIoC.Resolve<IFoo>(); // will recursively resolve bar and pass to constructor 

To declare a type with property injection:

  pIoC.Register<IBar>(ioc => new SimpleBar());
 pIoC.Register<IFoo>(ioc => new SimpleFoo { Bar = ioc.Resolve<IBar>() });
 var actual = pIoC.Resolve<IFoo>(); 

Note this is possible for any level of dependency:

 pIoC.Register<IGamma>(ioc => new Gamma());
 pIoC.Register<IBeta>(ioc => new Beta(ioc.Resolve<IGamma>()); 
 pIoC.Register<IAlpha>(ioc => new Alpha(ioc.Resolve<IBeta>()); // this will resolve IGamma to resolve IBeta 

Last edited Nov 28, 2012 at 2:19 PM by jeremylikness, version 6


No comments yet.